-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
119 lines (108 loc) · 3.1 KB
/
Copy pathindex.html
File metadata and controls
119 lines (108 loc) · 3.1 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Smart Form Validation POC</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 2rem;
}
.form-group {
margin-bottom: 1.5rem;
}
input {
padding: 0.5rem;
font-size: 1rem;
width: 100%;
}
.error {
color: red;
margin-top: 0.3rem;
font-size: 0.9rem;
}
</style>
</head>
<body>
<h2>Smart Validation Form</h2>
<div class="form-group">
<label>Email Address</label>
<input type="text" id="emailInput" placeholder="Enter your email" />
<div class="error" id="emailError"></div>
</div>
<div class="form-group">
<label>Credit Card Number</label>
<input type="text" id="cardInput" placeholder="Enter your credit card number" />
<div class="error" id="cardError"></div>
</div>
<div class="form-group">
<label>Aadhaar Number</label>
<input type="text" id="aadhaarInput" placeholder="Enter your Aadhaar number" />
<div class="error" id="aadhaarError"></div>
</div>
<div class="form-group">
<label>Mobile Number</label>
<input type="text" id="mobileInput" placeholder="Enter your mobile number" />
<div class="error" id="mobileError"></div>
</div>
<script>
const validators = [
{
inputId: "emailInput",
errorId: "emailError",
regex: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
errorText: "⚠️ Please enter a valid email address"
},
{
inputId: "cardInput",
errorId: "cardError",
regex: /^\d{16}$/,
errorText: "⚠️ Credit card number must be 16 digits"
},
{
inputId: "aadhaarInput",
errorId: "aadhaarError",
regex: /^\d{12}$/,
errorText: "⚠️ Aadhaar number must be 12 digits"
},
{
inputId: "mobileInput",
errorId: "mobileError",
regex: /^[6-9]\d{9}$/,
errorText: "⚠️ Mobile number must be 10 digits and start with 6-9"
}
];
let isPageVisible = true;
document.addEventListener("visibilitychange", () => {
isPageVisible = !document.hidden;
});
validators.forEach(({ inputId, errorId, regex, errorText }) => {
const input = document.getElementById(inputId);
const error = document.getElementById(errorId);
let typingTimer, lastKeyTime = 0;
input.addEventListener("input", () => {
const now = Date.now();
const typingSpeed = now - lastKeyTime;
lastKeyTime = now;
clearTimeout(typingTimer);
error.textContent = "";
const wait = Math.min(Math.max(typingSpeed * 1.5, 800), 2000);
typingTimer = setTimeout(() => {
if (!isPageVisible) return;
const val = input.value.trim();
if (!val) {
error.textContent = "";
return;
}
if (!regex.test(val)) {
error.textContent = errorText;
} else {
error.textContent = "";
}
}, wait);
});
});
</script>
</body>
</html>