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  ||  ✨ 10x 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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

9 REPLIES 9

@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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Its working as expected ankur. Thanks so much for your help😊

Glad to help.

 

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

Very kind of you😊

mohanmerava
Tera Contributor

@Ankur Bawiskar  with the above. Code it is correctly validating for single email domain. With below code you shared it accepting all domains. Please help me on this ankur