-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_form.js
87 lines (68 loc) · 2.86 KB
/
validate_form.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
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
// validate_form.js
document.addEventListener("DOMContentLoaded", function() {
var form = document.getElementById("registrationForm");
form.addEventListener("submit", function(event) {
if (!validateForm()) {
event.preventDefault(); // Prevent form submission if validation fails
}
});
function validateForm() {
var user_id = document.getElementById("user_id").value;
var name = document.getElementById("name").value;
var pass = document.getElementById("pass").value;
var phone_no = document.getElementById("phone_no").value;
var dob = document.getElementById("dob").value;
var gender = document.getElementById("gender").value;
var city = document.getElementById("city").value;
if (user_id === "" || name === "" || pass === "" || phone_no === "" || dob === "" || gender === "" || city === "") {
alert("All fields must be filled out");
return false;
}
if (!isAlphanumeric(user_id)) {
alert("User ID must be alphanumeric");
return false;
}
if (!isAlphabetic(name)) {
alert("Name must contain only alphabetic characters");
return false;
}
if (!isValidPassword(pass)) {
alert("Password must be at least 8 characters and contain at least one alphabet, one numeric character, and one special character.");
return false;
}
if (!isNumeric(phone_no)) {
alert("Phone number must be numeric");
return false;
}
if (!isValidDate(dob)) {
alert("Invalid date of birth format");
return false;
}
if (!isAlphabetic(city)) {
alert("City must contain only alphabetic characters");
return false;
}
return true; // Form is valid
}
function isAlphanumeric(input) {
var alphanumericRegex = /^[a-zA-Z0-9]+$/;
return alphanumericRegex.test(input);
}
function isNumeric(input) {
var numericRegex = /^\d+$/;
return numericRegex.test(input);
}
function isValidDate(input) {
var dateRegex = /^\d{4}-\d{2}-\d{2}$/;
return dateRegex.test(input);
}
function isValidPassword(input) {
// Password must be at least 8 characters and contain at least one alphabet, one numeric character, and one special character
var passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$/;
return passwordRegex.test(input);
}
function isAlphabetic(input) {
var alphabeticRegex = /^[a-zA-Z\s]+$/;
return alphabeticRegex.test(input);
}
});