-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (88 loc) · 3.5 KB
/
script.js
File metadata and controls
129 lines (88 loc) · 3.5 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
$(document).ready(function() {
$("#togglePassword").click(function() {
const passwordField = $("#password");
const type = passwordField.attr("type") === "password" ? "text" : "password";
passwordField.attr("type", type);
$(this).text(type === "password" ? "Show Password" : "Hide Password");
});
$("#toggleCnfPassword").click(function() {
const cnfPasswordField = $("#cnf-password");
const type = cnfPasswordField.attr("type") === "password" ? "text" : "password";
cnfPasswordField.attr("type", type);
$(this).text(type === "password" ? "Show Confirm Password" : "Hide Confirm Password");
});
// Prevent non-digit characters in phone input
$("#phone").on("keypress", function(e) {
const charCode = e.which ? e.which : e.keyCode;
// Allow only digits (0-9), charCode 48-57
if (charCode < 48 || charCode > 57) {
e.preventDefault();
}
});
// Also prevent pasting non-digit characters
$("#phone").on("paste", function(e) {
const pasteData = e.originalEvent.clipboardData.getData('text');
if (!/^\d+$/.test(pasteData)) {
e.preventDefault();
}
});
// Prevent non-digit characters and restrict to 10 digits in phone input
$("#phone").on("keypress", function(e) {
const charCode = e.which ? e.which : e.keyCode;
const inputValue = $(this).val();
// Allow only digits (0-9), charCode 48-57
if (charCode < 48 || charCode > 57 || inputValue.length >= 10) {
e.preventDefault();
}
});
// Also prevent pasting more than 10 digits or non-digit characters
$("#phone").on("paste", function(e) {
const pasteData = e.originalEvent.clipboardData.getData('text');
const inputValue = $(this).val();
if (!/^\d+$/.test(pasteData) || (inputValue.length + pasteData.length > 10)) {
e.preventDefault();
}
});
function isValidEmail(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
function isValidPhone(phone) {
return /^\d{10}$/.test(phone);
}
function isStrongPassword(password){
return /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/.test(password);
}
function showMessage(msg, type) {
const messageBox = $("#message");
messageBox.removeClass().addClass(`message ${type}`).text(msg).show();
}
$("#myForm").submit(function(e) {
e.preventDefault();
const name = $("#name").val().trim();
const email = $("#email").val().trim();
const phone = $("#phone").val().trim();
const password = $("#password").val().trim();
if(!name || !email || !phone || !password) {
showMessage("All fields are required.", "error");
return;
}
if(!isValidEmail(email) ){
showMessage("please enter a valid email address.", "error")
return;
}
if(!isValidPhone(phone)){
showMessage("Phone number must be 10 digits.", "error");
return;
}
if($("#password").val() !== $("#cnf-password").val()) {
showMessage("Passwords do not match.", "error");
return;
}
if(!isStrongPassword(password)){
showMessage("Password must contain uppercase, lowercase, digit, and be 6+ characters with one special charachter.", "error");
return;
}
showMessage("Form submitted successfully!", "success");
})
});