0

I am using the follwoing regular expression for email validation

@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"

Bit it accepts []name@gmail.com[][] as a valid email.whats the pattern i should use? Is it possible to check that at client side?

Amol
  • 1,431
  • 2
  • 18
  • 32
  • 7
    [I Knew How To Validate An Email Address Until I Read The RFC](http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx) – Habib Jan 24 '13 at 09:26
  • Did you try to search a little on the subject before posting your question? – Yannick Blondeau Jan 24 '13 at 09:27
  • Dear @YannickBlondeau I tried a lot but when i entered before or later valid email address it accepts as a valid email – Amol Jan 24 '13 at 09:30
  • 1
    possible duplicate of [Email Validation - Regular Expression](http://stackoverflow.com/questions/1903356/email-validation-regular-expression) – Anirudha Jan 24 '13 at 09:32
  • @Amol, I meant searching on StackOverflow to see if your question has already been answered... – Yannick Blondeau Jan 24 '13 at 09:50
  • @YannickBlondeau yes but none of regex solve my problem i want this problem solve at client side to avoid user headac that after submit he knows that entered email address is wrong – Amol Jan 24 '13 at 09:56
  • possible duplicate of [Using a regular expression to validate an email address](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) – Lie Ryan Jan 25 '13 at 09:23

1 Answers1

2

If you want to validate an Email Address Regex is not the right choice.

Use MailAddress as recommended by SLaks

try 
{
   address = new MailAddress(address).Address;
   //address is valid here
} 
catch(FormatException) 
{
   //address is invalid
}

But if you are addicted to regex..just do this

.*@.*
Community
  • 1
  • 1
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • Hello, is it possible to check at client side? – Amol Jan 24 '13 at 09:38
  • 1
    @Amol not possible since there are many corner cases where the regex would fail..you can try `.*@.*` at client side and then check it through `MailAddress` at server side.. – Anirudha Jan 24 '13 at 09:41