Home All Groups Group Topic Archive Search About

email validation strings



Author
25 Oct 2007 2:13 PM
jp2code
I've got a couple of strings I use to validate emails.

I have no idea what either of them do or what they mean! I found them, they
work, so I use them!

Could somebody let me know what these mean? My guess is that the longer
string is better, simply because it tests more thoroughly, but I could be
mistaken.

String1 =
""^([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})(\]?)$""

String2 = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w{2,}$"

Thanks!
~Joe

Author
25 Oct 2007 4:30 PM
Tim Slattery
Show quote
"jp2code" <poojo.com/mail> wrote:

>I've got a couple of strings I use to validate emails.
>
>I have no idea what either of them do or what they mean! I found them, they
>work, so I use them!
>
>Could somebody let me know what these mean? My guess is that the longer
>string is better, simply because it tests more thoroughly, but I could be
>mistaken.
>
>String1 =
>""^([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})(\]?)$""
>
>String2 = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w{2,}$"

They are regular expressions. If you don't know what those are, look
here: http://www.regular-expressions.info/quickstart.html

The first looks for any combination of letters, digits, hyphens, and
dots followed by @, followed by either an IP address surrounded by
square brackets, or the more familiar domain name. So it would accept
either a**@xyz.com or abc@[123.222.11.143], which are both legal email
addresses.

The second is simpler. "\w" is a "word" character (which I think is
letters or digits), so it's at least one of these, followed by any
combination of "word character", hyphen, plus sign, dot, ending with
at least one "word character". Then @, then a domain name (less
rigorously scanned then in the first RE.

So the first one allows IP addresses behind the @ sign, which is legal
but *very* rare. Both, IMHO, will catch 99% of malformed email
addresses.

--
Tim Slattery
MS MVP(DTS)
Slatter***@bls.gov
http://members.cox.net/slatteryt
Author
25 Oct 2007 6:30 PM
jp2code
Thanks Mr. Slattery!

I knew they were "regular expressions," but they were a lot more complicated
looking that the standard [0-9] or [A-Z] that I've ever had to use in the
past.

Regards,
~Joe

Show quote
"Tim Slattery" wrote:
> They are regular expressions. If you don't know what those are, look
> here: http://www.regular-expressions.info/quickstart.html
>
> The first looks for any combination of letters, digits, hyphens, and
> dots followed by @, followed by either an IP address surrounded by
> square brackets, or the more familiar domain name. So it would accept
> either a**@xyz.com or abc@[123.222.11.143], which are both legal email
> addresses.
>
> The second is simpler. "\w" is a "word" character (which I think is
> letters or digits), so it's at least one of these, followed by any
> combination of "word character", hyphen, plus sign, dot, ending with
> at least one "word character". Then @, then a domain name (less
> rigorously scanned then in the first RE.
>
> So the first one allows IP addresses behind the @ sign, which is legal
> but *very* rare. Both, IMHO, will catch 99% of malformed email
> addresses.
>
> --
> Tim Slattery
> MS MVP(DTS)
> Slatter***@bls.gov
> http://members.cox.net/slatteryt

AddThis Social Bookmark Button