Skip to content

Commit 2ad4dcc

Browse files
Feature/new snippet (#2395)
* Add dedicated README for onChange email validation script * Add onChange email validation script for real-time feedback
1 parent 1a9399a commit 2ad4dcc

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ServiceNow Email Validation - onChange Script
2+
3+
## Overview
4+
Real-time email validation script for ServiceNow catalog items that provides instant feedback as users type in email fields.
5+
6+
## Features
7+
- Real-time validation during user input
8+
- Instant error/success messages
9+
- Dynamic message clearing based on validity
10+
- User-friendly feedback with examples
11+
- No form submission required for validation
12+
13+
## Implementation
14+
1. Navigate to **Service Catalog > Catalog Client Scripts**
15+
2. Click **New** to create a new script
16+
3. Select your **Catalog Item**
17+
4. Set **Type** to **onChange**
18+
5. Set **Variable name** to your email field variable
19+
6. Replace `email_field_name` with actual variable name in script
20+
7. Paste the script code
21+
8. Check **Active** checkbox
22+
9. Save
23+
24+
## Validation Rules
25+
- Required format: `user@domain.com`
26+
- Accepts: letters, numbers, dots (.), underscores (_), hyphens (-)
27+
- Minimum 2-character top-level domain
28+
29+
## User Experience
30+
- **Invalid Input**: Red error message displayed below field
31+
- **Valid Input**: Green info message confirming valid format
32+
- **Empty Field**: No validation performed
33+
- **While Loading**: Validation skipped to prevent false errors
34+
35+
## Benefits
36+
- Immediate feedback reduces user frustration
37+
- Prevents errors before form submission
38+
- Improves data quality
39+
- Enhances overall user experience
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+
}

0 commit comments

Comments
 (0)