diff --git a/Client-Side Components/Catalog Client Script/Email Validation/email_validation_onChange.js b/Client-Side Components/Catalog Client Script/Email Validation/email_validation_onChange.js new file mode 100644 index 0000000000..ee48ee033c --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Email Validation/email_validation_onChange.js @@ -0,0 +1,17 @@ +function onChange(control, oldValue, newValue, isLoading) { + if (isLoading || newValue === '') { + return; + } + + // 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(newValue)) { + g_form.showFieldMsg('email_field_name', 'Invalid email format. Example: user@example.com', 'error'); + g_form.hideFieldMsg('email_field_name', true); // Clear info messages + } else { + g_form.hideFieldMsg('email_field_name'); // Clear error message + g_form.showFieldMsg('email_field_name', 'Valid email address', 'info'); + } +} \ No newline at end of file diff --git a/Client-Side Components/Catalog Client Script/Email Validation/email_validation_onSubmit.js b/Client-Side Components/Catalog Client Script/Email Validation/email_validation_onSubmit.js new file mode 100644 index 0000000000..25e47cd289 --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Email Validation/email_validation_onSubmit.js @@ -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 +} \ No newline at end of file diff --git a/Client-Side Components/Catalog Client Script/Email Validation/readme_email_validation.md b/Client-Side Components/Catalog Client Script/Email Validation/readme_email_validation.md new file mode 100644 index 0000000000..71e699dbfe --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Email Validation/readme_email_validation.md @@ -0,0 +1,29 @@ +# ServiceNow Email Validation Catalog Client Script + +## Overview +Client-side validation script for ServiceNow catalog items to ensure proper email format in user input fields. + +## Features +- Real-time email format validation +- Form submission prevention for invalid emails +- User-friendly error messages +- Standard email pattern validation + +## Implementation +1. Navigate to **Service Catalog > Catalog Client Scripts** +2. Create new script and select catalog item +3. Choose script type: **onSubmit** or **onChange** +4. Replace `email_field_name` with your field variable name +5. Save and activate + +## Validation Rules +- Required format: `user@domain.com` +- Accepts: letters, numbers, dots, underscores, hyphens +- Minimum 2-character domain extension + +## Script Types +- **onSubmit**: Validates before form submission +- **onChange**: Real-time validation as user types + +## Usage +Best practice: Use both scripts for comprehensive validation and better user experience.