Skip to content

Commit b8f45a2

Browse files
Create Validate PhoneNumber.js
1 parent 8859b81 commit b8f45a2

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function onChange(control, oldValue, newValue, isLoading) {
2+
if (isLoading || !newValue) return;
3+
4+
var fieldName = control.name;
5+
var fieldLabel = g_form.getLabelOf ? g_form.getLabelOf(fieldName) : fieldName;
6+
7+
// --- Phone format: (123) 456-7890 ---
8+
var phonePattern = /^\(\d{3}\) \d{3}-\d{4}$/;
9+
10+
// Clear any existing field messages
11+
if (g_form.hideFieldMsg) g_form.hideFieldMsg(fieldName, true);
12+
13+
// Validate the phone number
14+
if (!phonePattern.test(newValue)) {
15+
// Reset invalid input
16+
if (g_form.setValue) {
17+
g_form.setValue(fieldName, '');
18+
} else {
19+
control.value = '';
20+
}
21+
22+
// Show a clear inline message below the field
23+
if (g_form.showFieldMsg) {
24+
g_form.showFieldMsg(
25+
fieldName,
26+
'Phone Number must be in the format (123) 456-7890',
27+
'error',
28+
false // false = show below the field
29+
);
30+
} else {
31+
alert(fieldLabel + ' must be in the format (123) 456-7890');
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)