0

I am using javascript to validate email but its giving Parsing error in regex

var regex = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]­{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');

Parser Error Message: "[" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.

Thanks

SMI
  • 303
  • 1
  • 6
  • 22
  • Why don't you try this way [Validate email address in JavaScript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Harish Talanki Jan 23 '14 at 11:11
  • @HarishTalanki This SO answer is the generic way... may not be suited for OP's business rules. – Stephan Jan 23 '14 at 11:13
  • 1
    @HarishTalanki tried that too.. again ended up with error.. – SMI Jan 23 '14 at 11:16

4 Answers4

0

Your regex should look like this:

^[a-z0-9]+([_.-][a-z0-9]+)*@[a-z0-9]+([_.-]­[a-z0-9]+)*\.(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$

Demo

http://regex101.com/r/xK3cL4

Stephan
  • 41,764
  • 65
  • 238
  • 329
0

This regex is perfect and works fine for me.. try this.

/^(([^<>()[\]\\.,;:\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,}))$/
Vignesh
  • 1,045
  • 2
  • 17
  • 34
0

It needed little bit googling. I was thinking MVC and NTire ASP.Net application need same Regular Expression, it was not the case, just replaced @ with @@ and it worked. :) thank for your time @Alex :)

SMI
  • 303
  • 1
  • 6
  • 22
-3

Can you provide more of the file containing this? If it is being processed through an engine that uses the @ tag to mark code (like Razor) it may be that this regex is causing a parse error on the server before the JavaScript is send to the client.

Razor already has @ tag, and if you need to embed @ characters (even in JavaScript regexes) that are passed to the server, you'd need to escape them using @@ instead of @, i.e., doubling them:

/^(([^<>()[\]\\.,;:\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,}))$/
Stuart Watt
  • 5,242
  • 2
  • 24
  • 31