-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
64 lines (54 loc) · 1.97 KB
/
validation.js
File metadata and controls
64 lines (54 loc) · 1.97 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
let submitBtn = document.getElementById('submitBtn');
main();
function main() {
let form = document.getElementById("userInfo");
let formInputs = form.querySelectorAll('input');
for (let input of formInputs)
input.addEventListener('blur', event => {
try { validateInput(event.target); }
catch(err) { showError(event.target, err); return }
clearError(input);
});
}
function validateInput(input) {
let inputValue = input.value, match;
if (inputValue.trim() == false && input.id != 'dateOfBirth') {
input.value = ""; throw new Error("This field cannot be empty...");
}
if (/Name$/.test(input.id)) {
if (match = /[^a-z\s]/gi.exec(inputValue)){
throw new Error(`Invalid Char: "${match[0]}"`);
}
} else if (/password$/gi.test(input.id)) {
if (inputValue.length < 8) {
throw new Error("Must contain atleast 8 characters...");
} else if (input.id == 'confirmPassword' &&
inputValue != document.getElementById('password').value) {
throw new Error("Does not match password...");
}
} else if (input.type == 'email') {
const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
if (match = emailRegex.exec(inputValue)) {
console.log("This match is somewhat valid.");
} else {
throw new Error("Invalid email address...");
}
}
}
function showError(input, error) {
input.className += " error";
document.querySelector(`label[for=${input.id}]`)
.className += ' error';
let output = document.getElementById('errorBox');
output.style.display = 'block';
output.textContent = error;
submitBtn.setAttribute("disabled", "disabled");
}
function clearError(input) {
input.className = '';
document.querySelector(`label[for='${input.id}']`)
.className = '';
let output = document.getElementById('errorBox');
output.style.display = 'none';
submitBtn.removeAttribute("disabled");
}