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

regex is correct but some issue in the script

Community Alums
Not applicable

Hi,

I wrote the regex for validating the email ID, but the problem is coming with respect to the code. Kindly help.

 

function onSubmit() {

	var email_ID = g_form.getValue("email");
	
	
		var testEmail = /^([a-z0-9A-Z\_\.]*)@([a-z\.]*)$/;

		if (!testEmail.test(email_ID)) {
			g_form.addErrorMessage("invalid email");
			return false;
		}
		return true;

 

Regards

Suman P.

12 REPLIES 12

Ah, so you need domain part of the email always lower case. What is the email address you are testing which is not working? 

 

Your regex (below) works in background script and it passes for 

'test@servicenow.com' and fails for 'test@Servicenow.com'... although, it also failed for 'test@service-now.com':
var testEmail = /^([a-z0-9A-Z\_\.]*)@([a-z]*)+\.[a-z]*$/;
if (!testEmail.test(email_ID))
    gs.print('Invalid email');
else
    gs.print('PASSED');

@Community Alums 

Try with below code once:

 

function onSubmit() {
    var email_ID = g_form.getValue("email");
    var testEmail = /^[a-zA-Z0-9._]+@[a-z]+\.[a-z]+$/;
    if (!testEmail.test(email_ID)) {
        g_form.addErrorMessage("Invalid email");
        return false;
    }
    return true;
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

maroon_byte
Mega Sage

For me, this regex worked:

var testEmail = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (!testEmail.test(email_ID))
    gs.print('Invalid email');
else
    gs.print('PASSED');
 
BTW, if you need to validate email on a catalog item, use Validation Regex on variable as seen below:
 
maroon_byte_0-1712688527245.png

 

Regards,

Sharad

Subhashis Ratna
Tera Guru

Hi @Community Alums 

I have updated the code , u can try this logic ,

function onSubmit() {
    var email_ID = g_form.getValue("email");
    var testEmail = /^(?![\.])[a-zA-Z0-9!#$%*\/?|^{}`~&'\\+\-=_.]+(?<![.]+)@(?![\.-])[a-zA-Z0-9-_\.]+(\.[a-zA-Z0-9_]+)$/;
    if (!testEmail.test(email_ID)) {
        g_form.addErrorMessage("Invalid email");
        return false;
    }
    return true;
}



You can find many regular expressions for email validation. However, you can try using an out-of-the-box (OOTB) regular expression.

SS - 1 

SubhashisRatna_0-1712726649038.png

SS - 2 : In the marked field, you can use your regex for email validation.

SubhashisRatna_1-1712726698814.png

 

If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

Thanks,
Subhashis Ratna

 



Amit Pandey
Kilo Sage

Hi @Community Alums 

 

Please have a loot at this-

function onSubmit() {
    var email_ID = g_form.getValue("email");
    var testEmail = /^[a-zA-Z0-9._]+@[a-z]+\.[a-z]+$/;
    if (!testEmail.test(email_ID)) {
        g_form.addErrorMessage("Invalid email format");
        return false;
    }
    return true;
}

Regards,

Amit