2

I want to validate an email address using Spring Decoration of DOJO framework. but the regex which used to work in normal validation is not working.

<script type="text/javascript">
Spring.addDecoration(new Spring.ElementDecoration({
                        elementId: "emailAddress",
                        widgetType: "dijit.form.ValidationTextBox",
widgetAttrs: 
    {                             
    regExp : "/^[\w\.=-]+@[\w\.-]+\.[\w]{2,4}$/",
    required:"true",                         
    invalidMessage:"<span class='graytext'>Invalid format for email Address.<br> </span>",
    trim:"true"
                        }
                        }));

Why is my regExp is not working here in the Spring decoration of DOJO?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • With which email addresses are you testing it? That regex will fail on some valid ones. – Biffen May 25 '15 at 08:48
  • See http://stackoverflow.com/a/8829363/1587329 for a stackoverflow-approved email regex ;-): spoiler: ^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$ (had a backtick, so formatting cannot be done via backtick) – serv-inc May 25 '15 at 08:57
  • I'm trying to test a simple mail id: amit.kamath@gmail.com – Code learner May 25 '15 at 09:03
  • The regExp : "^[\\w\\-]([\\.\\w\\-\\'])*[\\w]*[@]([A-Za-z0-9]{1}[\\.\\w_-]*)[.][A-Za-z]{2,10}", works well. But I dont want to keep a highly restrictive validation. I want to use the other regex. – Code learner May 25 '15 at 09:10
  • 1
    That regex won't match ant new top level domains like .travel - consider scrapping it altogether – scrowler May 25 '15 at 09:36
  • If anyone wonders later about backticks inside backticks, you can [use multiple backticks](http://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks/251362#251362): ``^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0‌​-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$`` – serv-inc May 25 '15 at 10:09

2 Answers2

2

All you need to make this regex work is remove the regex delimiters (leading and trailing /) since the DOJO regexp already escapes the special regex characters:

regExp : "^[\w.=-]+@[\w.-]+\.\w{2,4}$"

Also, inside a character class, you do not have to escape the . and you do not have to enclose single \w into a character class.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

Try by adding double backslashes: "/^[\\w\\.=-]+@[\\w\\.-]+\.[\\w]{2,4}$/"

andale
  • 463
  • 2
  • 13