Passed
Pull Request — master (#223)
by Daniel
01:36
created

scripts/classes/ValidText/classValidText.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 35
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 25
mnd 1
bc 1
fnc 0
dl 0
loc 35
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
interface InputEvent extends Event {
2
  target: HTMLInputElement;
3
}
4
5
interface SubmitButton extends HTMLElement {
6
  removeAttribute(name: string): void;
7
  setAttribute(name: string, value: string): void;
8
}
9
10
/**
11
 * checkValidText is where we check if the text input is valid
12
 * If it is, we enable the submit button
13
 */
14
const checkValidText = (event: InputEvent): void => {
15
  const bilInformasjon = event.target.value;
16
  const submitButton = window.document.getElementById(
17
    "submitButton"
18
  ) as SubmitButton;
19
20
  const letters = /[A-Z]{2}\d{5}/gi;
21
  const bilInformasjonMatchesFormat = letters.test(bilInformasjon);
22
23
  if (
24
    bilInformasjonMatchesFormat &&
25
    bilInformasjon !== undefined &&
26
    bilInformasjon.length === 7
27
  ) {
28
    submitButton.removeAttribute("disabled");
29
  } else {
30
    submitButton.setAttribute("disabled", "true");
31
  }
32
};
33
34
export default checkValidText;
35