-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (69 loc) · 2.15 KB
/
script.js
File metadata and controls
79 lines (69 loc) · 2.15 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
// Event Listeners to fetch the button and the input.
const submitBtn = document.getElementById('btn');
const userName = document.getElementById('input__userName');
const email = document.getElementById('input__email');
const phone = document.getElementById('input__phone');
const password = document.getElementById('input__password');
const cPassword = document.getElementById('input__c-password');
//Predefined validation values to be validate after the user input values.
let userNameIsValid = false;
let emailIsValid = false;
let phoneIsValid = false;
let passwordIsValid = false;
let cPasswordIsValid = false;
// Validation processes as per the user input for different fields.
userName.addEventListener('blur', () => {
const regex = /^[\w@\-]{5,20}$/;
const str = userName.value;
// console.log(regex.exec(str));
if (regex.exec(str)) {
userNameIsValid = true;
} else {
userNameIsValid = false;
}
})
email.addEventListener('blur', () => {
const regex = /^[\d\w\-\.\&]{5,30}[@][a-z]{5,11}[\.][a-z]{3,7}$/;
const str = email.value;
// console.log(regex.exec(str));
if (regex.exec(str)) {
emailIsValid = true;
} else {
emailIsValid = false;
}
})
phone.addEventListener('blur', () => {
const regex = /^\d{10}$/;
const str = phone.value;
// console.log(regex.exec(str));
if (regex.exec(str)) {
phoneIsValid = true;
} else {
phoneIsValid = false;
}
})
password.addEventListener('blur', () => {
const regex = /^[\d\w\.\-@!#%^&*()=\+\[\]\|\\'";:/\?,<>`~ ]{6,}/;
const str = password.value;
// console.log(regex.exec(str));
if (regex.exec(str)) {
passwordIsValid = true;
} else {
passwordIsValid = false;
}
})
cPassword.addEventListener('blur', () => {
if (password.value === cPassword.value && password.length === cPassword.length) {
cPasswordIsValid = true;
} else {
cPasswordIsValid = false;
}
})
// Event Listener to validate the user input values.
submitBtn.addEventListener('click', () => {
if (userNameIsValid && emailIsValid && phoneIsValid && passwordIsValid && cPasswordIsValid) {
alert('Form Submitted Successfully.');
} else {
alert('Form is not valid.Please try again.');
}
})