Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

First hand at RegEx

 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys

I need to do some validation on my system registration screen and decided to use RegEx to validate the email address.

I found a handy little website that allows you to edit and test RegEx to see if you get the correct resulst.
mine should bascially allow for something like

This is what I came up with



It all works fine, apart for some reason it doesn't like it if you put a - in your email!
From what I can see it should allow

but it doesn't!

Confussed!

Any suggestions?

KS
[ June 26, 2008: Message edited by: Keith Seller ]
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try putting the hyphen first, like [-a-zA-Z0-9\\_\\.]

But: If you try to make the regexp overly specific it can happen all too easily that it disallows some proper addresses. I generally use "^\\S+@([-\\w]+\\.){1,4}[a-z]{2,6}$" with a few additional checks (like a valid TLD).

One problems I see with the one you posted is that various valid TLDs that have more than 3 characters (like .travel, .museum and .name) would fail to be recognized.
[ June 26, 2008: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic