Jump to content

Recommended Posts

How do I make a reg expression for email validation? This is what I have:

 

/(\da-zA-Z)+@(\da-zA-Z)+\.(a-ZA-Z)+/

 

So it says, check one or more of digits, alphabets, followed by @, followed by one or more digits,alphabets, followed by '.', followed by one or more alphabets, but it's not working. I'm using a forms plugin in WordPress, and we have to enter our own reg expressions.

 

Any help for my approach is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/257799-preg-stuff/#findComment-1321352
Share on other sites

How do I make a reg expression for email validation? This is what I have:

 

/(\da-zA-Z)+@(\da-zA-Z)+\.(a-ZA-Z)+/

 

So it says, check one or more of digits, alphabets, followed by @, followed by one or more digits,alphabets, followed by '.', followed by one or more alphabets, but it's not working. I'm using a forms plugin in WordPress, and we have to enter our own reg expressions.

 

Any help for my approach is appreciated.

 

That is not what that regex says at all. You do not have your characters grouped in character classes. [a-zA-Z\d]

The regex that you have is not doing what you think it is, and no valid email will pass that.

Link to comment
https://forums.phpfreaks.com/topic/257799-preg-stuff/#findComment-1321431
Share on other sites

Then what is round brackets for, and isn't square brackets imply OR, so [a-zA-Z\d]+ would mean one or more lower case a to z OR one or more upper case A to Z OR one or more digits?

What are "round brackets"?  I'm assuming you mean parentheses....

Parentheses are used to consolidate a regex search.  For instance, if you wanted to go through

and grab the name, class, and value as separate entities....

then you'd use parenthesis for each one.

 

The braces do not mean OR,,, they are used for ranges.. For example

[1-9A-Za-Z@%!]

Would only match something that had either of the characters.

453ABCzyx@@@%!!!!!  // Would match

453ABCzyx@@@%!!!!!$$  //Would not match because the $ is not within the range.

02356985  //Would not match because the numeric range is 1-9

Link to comment
https://forums.phpfreaks.com/topic/257799-preg-stuff/#findComment-1321712
Share on other sites

That pattern will match a standard email. But will also match things like test@test.helooooooooooooooooooooo (which apparently the code in this forum will match)

which clearly is not an email.

Also, there are numerous characters that are valid in email addresses that you have left out of your pattern (dashes, underscores, etc..)

post a sample string and your logic for matching the email address.

Link to comment
https://forums.phpfreaks.com/topic/257799-preg-stuff/#findComment-1321736
Share on other sites

preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);

will match an email

 

http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet-version-1/

this is a regex cheet sheet that i use explanes alot of stuff...

Link to comment
https://forums.phpfreaks.com/topic/257799-preg-stuff/#findComment-1322136
Share on other sites

preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);

will match an email

 

http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet-version-1/

this is a regex cheet sheet that i use explanes alot of stuff...

 

not to be rude, but that is one of the  grossest email regex I have seen in a while.

There are so many things wrong with it that I'm not even sure where to start.

That regex would match an "email" like test@test.com.com.com.com.com.com.org.co

This is one I like to use:

 

~^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$~i

 

depending on the context you could wrap it in word boundaries instead of anchors.

Link to comment
https://forums.phpfreaks.com/topic/257799-preg-stuff/#findComment-1322139
Share on other sites

yes but not all eamils are just i dot extention my old school one was @unb.nb.gc.ca ....

 

on the php manual some one cretaed this function

http://www.php.net/manual/en/function.preg-match.php#105542

looks complex but validates the entier address peace by peace

Link to comment
https://forums.phpfreaks.com/topic/257799-preg-stuff/#findComment-1322157
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.