Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ServiceNow Email Validation - onSubmit Script

## Overview
Email validation client script for ServiceNow catalog items that checks the email format when the user submits a form. This ensures that only valid email addresses are submitted and helps maintain data accuracy.

## Features
- Validates email during form submission
- Displays clear error messages near the field
- User-friendly feedback with examples
- Lightweight and client-side — no server calls required

## Implementation
1. Navigate to **Service Catalog > Catalog Client Scripts**
2. Click **New** to create a new script
3. Select your **Catalog Item**
4. Set **Type** to **onSubmit**
5. Set **Variable name** to your email field variable
6. Set UI Type to All
7. Replace `email_field_name` with actual variable name in script
8. Paste the script code
9. Check **Active** checkbox
10. Save

## Validation Rules
- Required format: `user@domain.com`
- Accepts: letters, numbers, dots (.), underscores (_), hyphens (-)
- Minimum 2-character top-level domain

## User Experience
- **Invalid Input**: Red error message displayed below field
- **Valid Input**: Form submission proceeds normally
- **Empty Field**: No validation performed
- **While Loading**: No validation triggered

## Benefits
- Prevents submission of incorrect email formats
- Provides errors(if any) before form submission
- Improves data quality
- Enhances overall user experience
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function onSubmit() {
// Get the email field value
var email = g_form.getValue('email_field_name'); // Replace 'email_field_name' with your actual field name

// Check if email field has a value
if (email) {
// Regular expression for email validation
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

// Test the email against the pattern
if (!emailPattern.test(email)) {
g_form.addErrorMessage('Please enter a valid email address');
g_form.showFieldMsg('email_field_name', 'Invalid email format. Example: user@example.com', 'error');
return false; // Prevent form submission
}
}

return true; // Allow form submission
}
Loading