The CreatorCon Call for Content is officially open! Get started here.

RegExp Email Validation is not working

VK13
Tera Expert

Hello all,

 

I am trying to build a basic regexp for email validation, but it's not working. Could you please what is wrong with the code.

*********************************************************************

var abc=g_form.getValue('email');
    var patt=/^[a-zA-Z0-9]\@[a-zA-Z0-9]\.[a-zA-Z0-9]$/;
     if(!(patt.test(abc)))
        alert('invalid email format');

*********************************************************************

I tested with xyz@gmail.com, but it's showing alert message. I tested by removing escape character "\", yet did not work.

9 REPLIES 9

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Guess there exists an OOB regex. Look for Service Catalog >> Catalog Variables >>Variable Validation Regex for Email.

If you are using validation for variable simply use the variable Regex in the Type specifictions tab of the variable.

Else, you can also try 

^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$

as email validation regex.

Since, you are using in script it will be

/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

 

 

Hi Jaspal,

 

Thanks. But could you please check why my above script is not working when I test it with abc@gmail.com?

There is an issue with the regex that is causing the issue. Look for the one suggested by Asif in comments it should work.

Also, if you prefer to have a breakdown & explanation of each then you can refer link where post entering the regex to the right you will have a explanation for the same.

Kunal Varkhede
Tera Guru

Hi,

 

Write onSubmit client script included OOB with that has following validation script

function onSubmit() {
   //Type appropriate comment here, and begin script below
   g_form.hideErrorBox('email');
   return validateEmail('email');
}

function validateEmail(field) { 
		var email = g_form.getValue(field);
		if (!email || email.trim().length <= 0)
			return true;
		var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		if (!re.test(email)) {
			g_form.showErrorBox(field, getMessage('Invalid Email'));
			return false;
		}
		return true;
	}

Go through this community link for your reference

https://community.servicenow.com/community?id=community_question&sys_id=83b88a49dbb7eb80feb1a851ca96...

 

Please mark correct/helpful answer if it help you in any way.

Thanks,

Kunal