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

scripts/classes/ValidText/classValidText.ts   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 39
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 28
mnd 3
bc 3
fnc 0
dl 0
loc 39
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
15
const checkValidText = (event: InputEvent): void => {
16
  const bilInformasjon = event?.target?.value;
17
  const submitButton = window.document.getElementById(
18
    "submitButton"
19
  ) as SubmitButton;
20
21
  const letters = /[A-Z]{2}\d{5}/gi;
22
  const bilInformasjonMatchesFormat = letters.test(bilInformasjon);
23
24
  console.log("bilInformasjon:", bilInformasjon);
25
  console.log("bilInformasjonMatchesFormat: ", bilInformasjonMatchesFormat);
26
27
  if (
28
    bilInformasjonMatchesFormat &&
29
    bilInformasjon !== undefined &&
30
    bilInformasjon.length === 7
31
  ) {
32
    submitButton.removeAttribute("disabled");
33
  } else {
34
    submitButton.setAttribute("disabled", "true");
35
  }
36
};
37
38
export default checkValidText;
39