-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumerical_validator.js
35 lines (34 loc) · 997 Bytes
/
numerical_validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* global document,window */
class NumericalValidator {
constructor(form, selector) {
this.form = form;
this.textboxInput = form.querySelector(selector.textboxInput);
this.textboxResult = form.querySelector(selector.textboxResult);
this.btnSubmit = form.querySelector(selector.btnSubmit);
}
init() {
this.bindElements();
}
bindElements() {
this.form.addEventListener("submit", (event) => {
this.textboxResult.value = this.isNumber();
if (!this.isNumber()) {
event.preventDefault();
}
});
}
isNumber() {
return ((!!this.textboxInput.value) && (!isNaN(this.textboxInput.value)));
}
}
window.onload = () => {
var selector = {
formSelector: "[data-value=form]",
textboxInput: "[data-value=input-text]",
textboxResult: "[data-value=result]",
btnSubmit: "[data-value=submit]"
};
var form = document.querySelector(selector.formSelector);
var obj = new NumericalValidator(form, selector);
obj.init();
};