From c4c816932bc420dedbc51537ec4c2839c94b431c Mon Sep 17 00:00:00 2001 From: Dhruv Yadav <43941048+Dhruvyadav2000@users.noreply.github.com> Date: Wed, 22 Oct 2025 14:29:52 +0530 Subject: [PATCH 1/2] Add README for email validation catalog client script --- .../readme_email_validation.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Client-Side Components/Catalog Client Script/Email Validation/readme_email_validation.md 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. From 09d0dacfe086bce3d5479cc1d5bd62ed115aed03 Mon Sep 17 00:00:00 2001 From: Dhruv Yadav <43941048+Dhruvyadav2000@users.noreply.github.com> Date: Wed, 22 Oct 2025 14:31:57 +0530 Subject: [PATCH 2/2] Add onchange/onSubmit client script email validation for catalog items Implement form submission validation for email fields in ServiceNow catalog items. --- .../email_validation_onChange.js | 17 +++++++++++++++++ .../email_validation_onSubmit.js | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 Client-Side Components/Catalog Client Script/Email Validation/email_validation_onChange.js create mode 100644 Client-Side Components/Catalog Client Script/Email Validation/email_validation_onSubmit.js 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