-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSFormValidation.js
More file actions
131 lines (115 loc) · 4.42 KB
/
JSFormValidation.js
File metadata and controls
131 lines (115 loc) · 4.42 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
window.onload = init;
function init() {
document.getElementById("formTest").onsubmit = validateForm;
document.getElementById("btnReset").onclick = clearForm;
document.getElementById("txtName").focus();
}
let myVar = setInterval(myTimer ,1000);
function myTimer() {
const d = new Date();
document.getElementById("demo").innerHTML = '<b>Time:</b>'+d.toLocaleTimeString();
}
function validateForm(theForm) {
with(theForm) {
return (isNotEmpty(txtName, "Please enter your name!", elmNameError)
&& isNumeric(txtZipcode, "Please enter a 5-digit zip code!", elmZipcodeError)
&& isLengthMinMax(txtZipcode, 5, 5, "Please enter a 5-digit zip code!", elmZipcodeError)
&& isSelected(selCountry, "Please make a selection!", elmCountryError)
&& isChecked("gender", "Please check a gender!", elmGenderError)
&& isAlphanumeric(txtpan, "InValid PAN!", elmPanError)
&& isChecked("color", "Please check a color!", elmColorError)
&& isNumeric(txtPhone, "Please enter a valid phone number!", elmPhoneError)
&& isValidEmail(txtEmail, "Enter a valid email!", elmEmailError)
&& isValidPassword(txtPassword, "Password shall be 6-8 characters!", elmPasswordError)
&& verifyPassword(txtPassword, txtPWVerified, "Different from new password!",elmPWVerifiedError)
);
}
}
function postValidate(isValid, errMsg, errElm, inputElm) {
if (!isValid) {
if (errElm !== undefined && errElm !== null
&& errMsg !== undefined && errMsg !== null) {
errElm.innerHTML = errMsg;
}
if (inputElm !== undefined && inputElm !== null) {
inputElm.classList.add("errorBox");
inputElm.focus();
}
} else {
if (errElm !== undefined && errElm !== null) {
errElm.innerHTML = "";
}
if (inputElm !== undefined && inputElm !== null) {
inputElm.classList.remove("errorBox");
}
}
}
function isNotEmpty(inputElm, errMsg, errElm) {
var isValid = (inputElm.value.trim() !== "");
postValidate(isValid, errMsg, errElm, inputElm);
return isValid;
}
function isNumeric(inputElm, errMsg, errElm) {
var isValid = (inputElm.value.trim().match(/^\d+$/) !== null);
postValidate(isValid, errMsg, errElm, inputElm);
return isValid;
}
function isAlphabetic(inputElm, errMsg, errElm) {
var isValid = (inputElm.value.trim().match(/^[a-zA-Z]+$/) !== null) ;
postValidate(isValid, errMsg, errElm, inputElm);
return isValid;
}
function isAlphanumeric(inputElm, errMsg, errElm) {
var isValid = (inputElm.value.trim().match(/^([a-zA-Z]{5})(\d{4})([a-zA-Z]{1})$/) !== null);
postValidate(isValid, errMsg, errElm, inputElm);
return isValid;
}
function isLengthMinMax(inputElm, minLength, maxLength, errMsg, errElm) {
var inputValue = inputElm.value.trim();
var isValid = (inputValue.length >= minLength) && (inputValue.length <= maxLength);
postValidate(isValid, errMsg, errElm, inputElm);
return isValid;
}
function isValidEmail(inputElm, errMsg, errElm) {
var isValid = (inputElm.value.trim().match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) !== null);
postValidate(isValid, errMsg, errElm, inputElm);
return isValid;
}
function isSelected(selectElm, errMsg, errElm) {
var isValid = (selectElm.value !== "");
postValidate(isValid, errMsg, errElm, selectElm);
return isValid;
}
function isChecked(inputName, errMsg, errElm) {
var elms = document.getElementsByName(inputName);
var isChecked = false;
for (var i = 0; i < elms.length; ++i) {
if (elms[i].checked) {
isChecked = true;
break;
}
}
postValidate(isChecked, errMsg, errElm, null);
return isChecked;
}
function isValidPassword(inputElm, errMsg, errElm) {
var isValid = (inputElm.value.trim().match(/^\w{6,8}$/) !== null);
postValidate(isValid, errMsg, errElm, inputElm);
return isValid;
}
function verifyPassword(pwElm, pwVerifiedElm, errMsg, errElm) {
var isTheSame = (pwElm.value === pwVerifiedElm.value);
postValidate(isTheSame, errMsg, errElm, pwVerifiedElm);
return isTheSame;
}
function clearForm() {
var elms = document.querySelectorAll('.errorBox'); // class
for (var i = 0; i < elms.length; i++) {
elms[i].classList.remove("errorBox");
}
elms = document.querySelectorAll('[id$="Error"]');
for (var i = 0; i < elms.length; i++) {
elms[i].innerHTML = "";
}
document.getElementById("txtName").focus();
}