Email validation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 05:55 AM
Hi Team,
As there is a need for our requirement, could you please help with creating new script with below conditions.
Email validation - Email that is ending with .AB should throw an error saying, 'Email ending with .AB are not accepted.'
Email must contain the value @, If it does not contain @ then throw an error saying "Email must contain @"
Spaces while entering email ID is not allowed, if it contains spaces then throw an error saying, "spaces are not allowed."
Also, if a check box is flagged, then make the email field ready only.
Thank you!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 06:08 AM
Please check the below given link.
https://www.servicenow.com/community/itsm-articles/validate-email-id-using-regex/ta-p/2307653
Hope this post helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 06:15 AM
You can write an onChange client script and mention below code
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
if (g_form.getValue("Check_BOX_backend_name")) {
g_form.setReadOnly('u_email', 'true');
}
return;
}
if (g_form.getValue("Check_BOX_backend_name")) {
g_form.setReadOnly('u_email', 'true');
}else{
g_form.setReadOnly('u_email', 'false');
}
if (newValue.endsWith('.AB')) {
alert('Email cannot end with .AB');
}
if (newValue.indexOf('@') == -1) {
alert('Email must contain @ symbol.');
}
if (/\s/.test(newValue)) {
alert('Spaces are not allowed in the email.');
}
}
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2023 02:15 AM
Question 1 :
How to add condition for validating email that email should not have space at the start and at the end and if we have space it should throw an error using client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 06:16 AM
Hi @Abhilasha_123
You can use a client script in ServiceNow to enforce these conditions on the email field. Below is an example script that you can use in the "onChange" client script of your email field.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var emailValue = g_form.getValue('your_email_field');
var checkBoxChecked = g_form.getValue('checkbox_field');
// Condition 1: Email ending with .AB
if (emailValue.toLowerCase().endsWith('.ab')) {
alert('Email ending with .AB is not accepted.');
g_form.setValue('your_email_field', oldValue);
return;
}
// Condition 2: Email must contain @
if (!emailValue.includes('@')) {
alert('Email must contain @');
g_form.setValue('your_email_field', oldValue);
return;
}
// Condition 3: Spaces are not allowed
if (emailValue.includes(' ')) {
alert('Spaces are not allowed in the email.');
g_form.setValue('your_email_field', oldValue);
return;
}
// Condition 4: Make the email field read-only if the checkbox is checked
g_form.setReadOnly('your_email_field', checkBoxChecked);
}
Please mark as accepted solution & hit helpful if it suffice your requirement
BR, Sohith