Question

I have a form, and when I submit it, I want to make an email validation before. I created a js file, put in theme's js directory, included it on the page and my page is WHITE. I activated the errors and I saw that this is the error:

SmartyCompilerException: Syntax Error in template "D:\Projects\mpm_shop\themes\pmart\js\processContactFormEmail.js" on line 44 "var re = /^([a-zA-Z0-9_.-+])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;" - Unexpected ",", expected one of: "}" , " " , ATTR in D:\Projects\mpm_shop\tools\smarty\sysplugins\smarty_internal_templatecompilerbase.php on line 667

My regex is this one:

var re = /^([a-zA-Z0-9_.-+])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

and I try to match it like this:

if (!email.match(re)) {
                    $('#email').css('border', '1px solid red'); 
                    $('.errorEmail').html('Email non è valido');
                    valid = false;
                }

Please help.

Was it helpful?

Solution

You have an error in your regex :

var re = /^([a-zA-Z0-9_.-+])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

In the first group, there is a dash between . and + (.-+), which is the cause for the error.

You simply need to escape it :

var re = /^([a-zA-Z0-9_.\-+])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

Next time you have regex issues, you can use an online regexp tool, like Regexr, which is very helpful to test and validate your expression !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top