Email validation

Abhilasha_123
Tera Contributor

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!!

4 REPLIES 4

Prashant Moily
Mega Sage

Prince Arora
Tera Sage
Tera Sage

@Abhilasha_123 

 

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.

 

 

 

 

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. 

Sohithanjan G
Kilo Sage
Kilo Sage

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

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)