-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
100 lines (90 loc) · 2.57 KB
/
validation.js
File metadata and controls
100 lines (90 loc) · 2.57 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
function validateForm() {
//declaring variables
var count = 0;
let user = document.infoForm.name.value;
let email = document.infoForm.email.value;
let phone = document.infoForm.phone.value;
let pass = document.infoForm.pass.value;
let conf_pass = document.infoForm.conf_pass.value;
let gender = document.infoForm.gender.value;
let age = document.infoForm.age.value;
const out = document.querySelector("#output");
out.innerText = "";
//for username validations
if (user == "") {
out.innerText += "Please enter username\n";
} else if (user.length < 4 || user.length > 12) {
out.innerText += "Please enter a valid username\n";
} else {
count++;
}
//for email validation
if (email == "") {
out.innerText += "Please enter email\n";
} else {
if (/^\S+@\S+\.\S+$/.test(email)) {
let ending = email.substring(email.length - 3);
if (ending == "edu" || ending == "com" || ending == "org" || ending == "net") {
count++;
} else {
out.innerText += "Please enter a valid email\n";
}
} else {
out.innerText += "Please enter a valid email\n";
}
}
//for phone number validation
if (phone == "") {
out.innerText += "Please enter phone number\n";
} else {
if(/^\(\d{3}\)-\d{3}-\d{4}$/.test(phone)) {
count++;
} else {
out.innerText += "Please enter a valid phone number\n";
}
}
//for password validation
if (pass == "") {
out.innerText += "Please enter password\n";
} else {
if (/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{8,}$/.test(pass)) {
count++;
} else {
out.innerText += "Please enter a valid password\n";
}
}
//for confirm password validation
if (conf_pass == "") {
out.innerText += "Please confirm password\n";
} else {
if (/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{8,}$/.test(conf_pass)) {
if (conf_pass == pass) {
count++;
} else {
out.innerText += "Please confirm password\n";
alert("Passwords do not match");
}
} else {
out.innerText += "Please confirm password\n";
}
}
//for gender validation
if (gender == "") {
out.innerText += "Please select gender\n";
} else {
count++;
}
//for age validation
if (age == "") {
out.innerText += "Please select age group\n";
} else {
count++;
}
if (count == 7) {
window.location.href = 'index.html'
}
}
function clearForm() {
document.getElementById("output").innerText = "";
}
//paragraph.textContent += "This just got added";