Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (127 loc) · 4.34 KB
/
Copy pathscript.js
File metadata and controls
151 lines (127 loc) · 4.34 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(function () {
'use strict';
const form = document.getElementById('regForm');
const successMsg = document.getElementById('successMsg');
function isValidName(v) { return /^[A-Za-z\s]+$/.test(v.trim()); }
function isEmail(v) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v); }
function isPhone(v) { return /^(09|\+639)\d{9}$/.test(v.replace(/\s/g, '')); }
function isZip(v) { return /^\d{4}$/.test(v.trim()); }
function isText(v) { return v.trim().length >= 2; }
function showError(input, msg) {
clearError(input);
const err = document.createElement('small');
err.className = 'error-msg';
err.textContent = msg;
input.parentElement.appendChild(err);
input.classList.add('invalid');
input.classList.remove('valid');
}
function clearError(input) {
const existing = input.parentElement.querySelector('.error-msg');
if (existing) existing.remove();
input.classList.remove('invalid');
}
function markValid(input) {
clearError(input);
input.classList.add('valid');
}
function validateField(input) {
const val = input.type === 'date' ? input.value : input.value.trim();
const name = input.name;
const required = input.hasAttribute('required');
if (!required && !val) {
clearError(input);
input.classList.remove('valid');
return true;
}
if (required && !val) {
showError(input, 'This field is required.');
return false;
}
if (input.type === 'email' && !isEmail(val)) {
showError(input, 'Enter a valid email (e.g. juan@email.com).');
return false;
}
if (name === 'phone' && !isPhone(val)) {
showError(input, 'Enter a valid PH mobile number (e.g. 09171234567).');
return false;
}
if (name === 'zipCode' && !isZip(val)) {
showError(input, 'ZIP code must be 4 digits (e.g. 1100).');
return false;
}
if (name === 'dob') {
const dob = new Date(val);
const today = new Date();
const minAge = new Date(today.getFullYear() - 18, today.getMonth(), today.getDate());
if (dob > minAge) {
showError(input, 'You must be at least 18 years old.');
return false;
}
}
if (['lastName', 'firstName', 'middleName'].includes(name)) {
if (!isValidName(val)) {
showError(input, 'Name must contain letters only.');
return false;
}
if (val.length < 2 && required) {
showError(input, 'Must be at least 2 characters.');
return false;
}
}
if (['barangay', 'city', 'province', 'street'].includes(name) && !isText(val)) {
showError(input, 'Must be at least 2 characters.');
return false;
}
markValid(input);
return true;
}
function initializeEventListeners() {
form.querySelectorAll('input, select').forEach((field) => {
field.addEventListener('input', () => clearError(field));
field.addEventListener('change', () => validateField(field));
field.addEventListener('blur', () => validateField(field));
});
}
function setDobMaxDate() {
const dobInput = document.querySelector('[name="dob"]');
if (dobInput) {
const today = new Date();
const maxDob = new Date(today.getFullYear() - 18, today.getMonth(), today.getDate());
dobInput.setAttribute('max', maxDob.toISOString().split('T')[0]);
}
}
function handleFormSubmit(e) {
e.preventDefault();
let allValid = true;
form.querySelectorAll('input[required], select[required]').forEach((field) => {
if (!validateField(field)) {
allValid = false;
}
});
if (!allValid) {
const firstInvalid = form.querySelector('.invalid');
if (firstInvalid) {
firstInvalid.focus();
firstInvalid.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
return;
}
form.style.display = 'none';
successMsg.classList.add('show');
}
function init() {
if (!form || !successMsg) {
console.error('Registration form or success message element not found');
return;
}
setDobMaxDate();
initializeEventListeners();
form.addEventListener('submit', handleFormSubmit);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();