Need to valid the email field based on domain email

mohanmerava
Tera Contributor

Hi Team,

 

 

 

How to write catalog client script to give email validation as it can accept only @(companyname).com users only. Need to valid it for 2 email domains. @(companyname1).com

 

 

 

Field is a single line text field(email Id).

 

@Ankur Bawiskar help on this used below code it is validating only one.  Need to validate 2 email domains

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

 if (isLoading) {

  return;

 }

 

 g_form.hideErrorBox('variable');

 

 var regex = /^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@companyname.com$/;

 

 newValue = newValue.toString();

 var isValidFormat = regex.test(newValue);

 

 if(!isValidFormat){

  g_form.showErrorBox('variable','Please enter valid email address','error');

  g_form.clearValue('variable');

 }

 

Thanks  

2 ACCEPTED SOLUTIONS

@mohanmerava

update as this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading) {
        return;
    }

    g_form.hideErrorBox('variable');
    var regex1 = /^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@companyname.com$/;
    var regex2 = /^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@companyname1.com$/;

    newValue = newValue.toString();
    var isValidFormat1 = regex1.test(newValue);
    var isValidFormat2 = regex2.test(newValue);
    if (!isValidFormat1 && !isValidFormat2) {
        g_form.showErrorBox('variable', 'Please enter valid email address', 'error');
        g_form.clearValue('variable');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

@mohanmerava 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@mohanmerava 

try this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    g_form.hideErrorBox('variable');

    // Regular expression to validate both domains
    var regex = /^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@(companyname\\.com|companyname1\\.com)$/;

    newValue = newValue.toString();
    var isValidFormat = regex.test(newValue);

    if (!isValidFormat) {
        g_form.showErrorBox('variable', 'Please enter a valid email address', 'error');
        g_form.clearValue('variable');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@mohanmerava 

try this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading) {
        return;
    }

    g_form.hideErrorBox('variable');
    var regex1 = /^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@companyname.com$/;
    var regex2 = /^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@companyname1.com$/;

    newValue = newValue.toString();
    var isValidFormat1 = regex1.test(newValue);
    var isValidFormat2 = regex2.test(newValue);
    if (!isValidFormat || !isValidFormat2) {
        g_form.showErrorBox('variable', 'Please enter valid email address', 'error');
        g_form.clearValue('variable');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Now it is not validating both email domains ankur. Can you recheck on this

@mohanmerava

update as this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading) {
        return;
    }

    g_form.hideErrorBox('variable');
    var regex1 = /^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@companyname.com$/;
    var regex2 = /^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@companyname1.com$/;

    newValue = newValue.toString();
    var isValidFormat1 = regex1.test(newValue);
    var isValidFormat2 = regex2.test(newValue);
    if (!isValidFormat1 && !isValidFormat2) {
        g_form.showErrorBox('variable', 'Please enter valid email address', 'error');
        g_form.clearValue('variable');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader