We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Need validate email id field on catalog item

mohanmerava
Tera Contributor

Hi Team,

 

 

 

 

 

 

 

How to write catalog client script to give email validation as it can accept only abc.ab@(companyname).com users only. Need to valid it for 2 email domains. Abc.ab @(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

2 ACCEPTED SOLUTIONS

@mohanmerava 

try this

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

    g_form.hideErrorBox('variable');

    // Updated regex to match the format abc.ab@company.com
    var regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

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

View solution in original post

@mohanmerava 

Sorry if you are asking the exact match then I already gave the code.

Please enhance it as per your requirement further.

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

View solution in original post

15 REPLIES 15

@mohanmerava

try this

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

    g_form.hideErrorBox('variable');

    // Updated regex to match only abc.ab@company.com
    var regex = /^abc\.ab@company\.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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar  it is not working as expected it is allowing everything. We need to accept only abc.ab@company.com

@mohanmerava 

should it be an exact match?

 

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

@mohanmerava 

then simply compare

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

    g_form.hideErrorBox('variable');
    if (newValue != 'abc.ab@company.com') {
        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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader