Regex validation for email addresses not working in client script

Tomi Corigliano
Kilo Sage

Since I cannot use the Question Regular Expression for a multi line text variable in a catalog item, I'm trying to do the reg ex validation for email addresses through a catalog client script. However I can't get it to work properly.

 

Can anyone please help me to get the right value for the regex. I tried several combinations

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	
	var regex = '-----BEGIN EMAIL VALIDATION-----^([a-zA-Z0-9])$-----END EMAIL VALIDATION-----';
	//Regex for RSA key (Private part)
	if(!newValue.match(regex)){  
		g_form.addErrorMessage('Please enter valid email addresses.');
	}
   
}

 

 

 

1 ACCEPTED SOLUTION

Tomi Corigliano
Kilo Sage

Thank you @Community Alums 

I had to alter the code a little bit to have it work:

 

if (!newValue.match(/^[a-zA-Z0-9\_\-\.]+@[a-zA-Z0-9\_\-\.]+\.[a-zA-Z]{2,5}$/)) {
   g_form.showFieldMsg("google_group_members_00","Please enter a valid email ID","Error");
}

View solution in original post

5 REPLIES 5

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Please check the below post on the same:-

 

https://www.servicenow.com/community/it-service-management-forum/email-validation-for-single-line-te...

 

Please mark my answer as correct based on Impact

Aman Kumar S
Kilo Patron

Hi @Tomi Corigliano ,

 

Below support article should assist you:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0661825

Best Regards
Aman Kumar

Community Alums
Not applicable

Hello @Tomi Corigliano 

 

Could you please try below code - 

 

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

    if (isLoading || newValue === '') {
        return;
    }

    if (!newValue.match(/^[a-zA-Z0-9_\-\.]+\@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}$/)) {

        g_form.addErrorMessage("Please enter a valid email ID");

    }

}

 

 

Muhammad Khan
Mega Sage
Mega Sage

If you navigate to Service Catalog > Catalog Variables > Variable Validation Regex, there you might Email regex. If that works for you then try utilizing that in your script. See the below image for reference.

MuhammadKhan_0-1667920936838.png