Skip to content

Commit 09d0dac

Browse files
Add onchange/onSubmit client script email validation for catalog items
Implement form submission validation for email fields in ServiceNow catalog items.
1 parent c4c8169 commit 09d0dac

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function onChange(control, oldValue, newValue, isLoading) {
2+
if (isLoading || newValue === '') {
3+
return;
4+
}
5+
6+
// Regular expression for email validation
7+
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
8+
9+
// Test the email against the pattern
10+
if (!emailPattern.test(newValue)) {
11+
g_form.showFieldMsg('email_field_name', 'Invalid email format. Example: user@example.com', 'error');
12+
g_form.hideFieldMsg('email_field_name', true); // Clear info messages
13+
} else {
14+
g_form.hideFieldMsg('email_field_name'); // Clear error message
15+
g_form.showFieldMsg('email_field_name', 'Valid email address', 'info');
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function onSubmit() {
2+
// Get the email field value
3+
var email = g_form.getValue('email_field_name'); // Replace 'email_field_name' with your actual field name
4+
5+
// Check if email field has a value
6+
if (email) {
7+
// Regular expression for email validation
8+
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
9+
10+
// Test the email against the pattern
11+
if (!emailPattern.test(email)) {
12+
g_form.addErrorMessage('Please enter a valid email address');
13+
g_form.showFieldMsg('email_field_name', 'Invalid email format. Example: user@example.com', 'error');
14+
return false; // Prevent form submission
15+
}
16+
}
17+
18+
return true; // Allow form submission
19+
}

0 commit comments

Comments
 (0)