RegExp Email Validation is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2020 07:55 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2020 07:59 AM
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+)*$/;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2020 09:24 AM
Hi Jaspal,
Thanks. But could you please check why my above script is not working when I test it with abc@gmail.com?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2020 11:27 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2020 08:21 AM
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
Please mark correct/helpful answer if it help you in any way.
Thanks,
Kunal