diff --git a/Client-Side Components/Catalog Client Script/Email & PhNo Validation/README.md b/Client-Side Components/Catalog Client Script/Email & PhNo Validation/README.md new file mode 100644 index 0000000000..53f9f1aa7b --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Email & PhNo Validation/README.md @@ -0,0 +1,11 @@ +This script validates the email and phone number fields entered by the user in a ServiceNow catalog item form. +It ensures that the email follows a valid format and the phone number is numeric with exactly 10 digits. + + +**Email Requirements:** +Must contain “@” and a valid domain (e.g., example.com). +No spaces or invalid characters allowed. + +**Phone Number Requirements:** +Must contain exactly 10 digits. +Only numeric values allowed (no letters or special characters). diff --git a/Client-Side Components/Catalog Client Script/Email & PhNo Validation/script.js b/Client-Side Components/Catalog Client Script/Email & PhNo Validation/script.js new file mode 100644 index 0000000000..70fbda4fdd --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Email & PhNo Validation/script.js @@ -0,0 +1,23 @@ +function onSubmit() { + // Get the field values + var email = g_form.getValue('email'); + var phone = g_form.getValue('phone_number'); + + // Define validation patterns + var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + var phonePattern = /^\d{10}$/; + + // Validate email format + if (!emailPattern.test(email)) { + g_form.showFieldMsg('email', 'Please enter a valid email address (e.g., user@example.com).', 'error'); + return false; // Prevent form submission + } + + // Validate phone number + if (!phonePattern.test(phone)) { + g_form.showFieldMsg('phone_number', 'Phone number must be exactly 10 digits and contain only numbers.', 'error'); + return false; // Prevent form submission + } + + return true; // Allow form submission if all validations pass +}