0

This is not working I am using following code to validate email for e.g

"email":{
"regex":"/^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/",
"alertText":"* Invalid email address"}, 

On this way I want to validate text box by regular expression. It should be Null or valid domain name like Google.com, example.com.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Manoj Patil
  • 35
  • 1
  • 1
  • 9

3 Answers3

0

If you just want to make it optional, that means empty string or a string that meets your regex, then try this:

^(?:[\w.-]+\@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,4})?$

I just put your regex into a non capturing group (?:)and make this group optional with the ? after the group.

And I simplified your character classes, [a-zA-Z0-9_] is the same than \w and escaping is for the most cases not needed within character clases.

stema
  • 90,351
  • 20
  • 107
  • 135
0

Producing a regular expression to validate an e-mail address is far from trivial.

You probably want to read something like this:

http://fightingforalostcause.net/misc/2006/compare-email-regex.php

ADW
  • 4,030
  • 17
  • 13
0

refer regular expression for email validation in Java

Community
  • 1
  • 1
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57