Jump to content

[SOLVED] validating email input


soycharliente

Recommended Posts

A friend gave me this code:

$pattern = "/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(([,]|[, ])\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*$/";

 

He said to just trust him that it works. He wouldn't try and help me understand what's going on. I mean, I'm fine with using it, but I'd like to know what's going on so I can learn. I know the basics of regex but nothing like this. I guess I'm just overwhelmed with all of the groupings.

 

I tried to look for this on-line, but I couldn't find anything remotely similar. I looked for one that I could understand, but it seems like everyone has a different pattern. People disagree on what valid characters are, how long certain parts should be, you should explode the parts and test them independently, etc.

 

I'm not interested in changing my code, just understanding what it does. It looks pretty complicated, but like I said, I only know the basics.

Any help or comments are MUCH appreciated.

Link to comment
Share on other sites

E-mail validation by regular expression is incredibly difficult/tedious if you go by the RFC definition and not just by "xxx@yyy.asdf.tld" common addresses.  That's why you see such disparity in e-mail matchers.

 

Perl has a nice feature, the /x modifier, that allows non-significant whitespace in regular expressions.  This allows you to break up a long or complex expression by putting it on many lines, indenting it, and including comments, therefore rendering it readable.  PHP includes this feature, too.

 

$pattern = '/^  # The beginning of the string
# The username part:
\w+             # one or more word characters
([-+.]\w+)*     # ...followed by (a dash, plus, or period and at least one more word character) zero or more times
# The "at" symbol:
@
# The domain part:
\w+             # one or more word characters
([-.]\w+)*      # ...followed by (a dash or period and at least one more word character) zero or more times
\.              # a literal period
\w+             # one or more word characters
([-.]\w+)*      # ...followed by (a dash or period and at least one more word character) zero or more times
# Repeat the whole thing zero or more times seperated by commas and/or spaces:
(([,]|[, ])\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*
$/x'; // The end.

 

I'd change this, though:

 

Should be:

(\s*,\s*) instead of ([,]|[, ])

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.