$string ='"Test User" <test#test.com>,"Another" <another#test.com>,"aeer" <whateveryourmail#gmail.com>';
I've succeed splitting this become pair of name and email address with this code
preg_match_all('!"(.*?)"\s+<\s*(.*?)\s*>!', $string, $matches);
the problem is, I can't validate the email address,false email address will be match too.
How to filter only valid email and also splitting Name and email address?
You could use some of the available mail address parsers available, e.g.:
mailparse_rfc822_parse_addresses()
Mail_RFC822::parseAddressList()
Optionally, filter the output through filter_var() (or one of its permutations targeted at arrays) with the FILTER_VALIDATE_EMAIL.
E-mail address validation can be done in two ways: check for a # and a . and assume it's valid. Users know their e-mail address, it is not our business if they enter a wrong one. If you want to be certain you make a good e-mail address checker, then there is only one correct regex to use for validation: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
What about jQuery?
http://plugins.jquery.com/project/jqueryvalidate
Related
I want to validate email address and website in comment box. When someone writes comment in comment box and after submission check if email address or website found in comment remove that email and address.
I have put below regular expression for email.
"/(?:[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/"
above expression validates email address but I want to validate like email[at]email[dot]com, email{at}email{dot}com, email(at)email(dot)com
Same for website validation I used below expression
"/((((http|https|ftp|ftps)\:\/\/)|www\.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}(\/\S*)?)/"
But I want to validate website like website[dot]com, www[dot]website[dot]com
Basically what you need to do is, where you have the validation of # and . character in email or . in weburl, you need to enhance your regex and put the alternatives to # character as you are expecting. So,
# should be written as (?:#|[[({]at[\]})])
And,
\. should be written as (?:\.|[[{(]dot[\]})])
wherever you have them in your regex and then it will also filter those strings as well.
Here is a modified regex for email.
(?:[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")(?:#|[[({]at[\]})])(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.|[[{(]dot[\]})]))+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
Regex Demo for email
Same way you can replace . from your website regex and your modified regex becomes this,
(?:(?:(?:http|https|ftp|ftps)\:\/\/)|www(?:\.|[[{(]dot[\]})]))(?:[a-zA-Z0-9.-]|[[{(]dot[\]})])+(?:\.|[[{(]dot[\]})])[a-zA-Z]{2,4}(\/\S*)?
Regex Demo for web url
Now besides matching of [dot], {dot} and (dot), the regex will also match [dot} and similar and as you are trying to detect such strings further, hence matching these strings will be an added advantage, rather than a problem unless the context was otherwise.
I would like to validate if an email address is entered by the user but I don't want to be too strict on it... So I came up with this....
My current if statement with regex:
if (preg_match('^/.+?#.+?\..+$/', $_POST["email"]))
However, I would not work for an email address with multiple periods...
For example:
testing#test.one.com
How can I allow multiple . or periods?
I would suggest something far simpler that doesn't require you to reinvent the wheel
if(filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to validate an email in php5?
I have used the following code to ensure email addresses provided on signup are valid.
(!preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*#( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email))
I entered a standard email address such as
yourname#company.co.uk
and it is flagging up as being invalid.
Can anyone help me with why this may be happening?
Why not just use filter_var
var_dump(filter_var($email,FILTER_VALIDATE_EMAIL));
EDIT
if(filter_var($email,FILTER_VALIDATE_EMAIL) === false)
{
echo 'Email is not valid';
}
else
{
//do the stuff
}
Now can I ask you a question? What are those spaces doing in your regular expression? :-)
I'm pretty certain that spaces aren't actually valid in email addresses. And, even if they were, they wouldn't be required to be at specific positions relative to the separators (such as immediately before and after the # character).
Although I generally disagree with the use of regular expressions for email addresses (just send an email with a confirmation link - that solves your problem and then some a), you should at least use the right regular expression if you must do it that way.
a There are an untold number of perfectly valid email addresses that don't have an actual account behind them.
if you would like to use regex for matching emails, the following will match "sensible" addresses.
preg_match('/^([a-z0-9]+([_\.\-]{1}[a-z0-9]+)*){1}([#]){1}([a-z0-9]+([_\-]{1}[a-z0-9]+)*)+(([\.]{1}[a-z]{2,6}){0,3}){1}$/i', $email)
It's quite verbose but if you only want, like i said "sensible" addresses to pass - it does the job.
does get stuck on address like "example#somename.somewhere.com" because, after the # symbol it looks for anything following a period to only be only 2-6 characters in length.
"example#somename-somewhere.com" however would pass fine.
I don't recommend trying to use a single regex solution for the job unless, as in my case, you only want to allow "sensible" addresses.
There is quite a good article that covers "correctly" validating email addresses here: http://www.linuxjournal.com/article/9585
With the new domainless addresses that are planned to be released, paxdiablo's solution seems even better
I use the Zend_Validate_EmailAddress to validate email addresses for my email program. It validates according to the RFC2822 - http://framework.zend.com/manual/en/zend.validate.set.html
My question is are these valid emails when they pass validation?
test#test.co.
test#test.co.za. etc Note the full stop at the end.
I find that the validator passes these email addresses which are obviously wrong. I don't fully understand why this should pass can anyone help me?
Regards
The email addresses are not "obviously wrong"; a DNS name is allowed to end with a trailing . to indicate that it's absolute rather than relative.
[EDITED to add: The above may be misleading. In an email address, for SMTP at least, hostnames are always interpreted as fully-qualified -- i.e., "absolute". So there's never a need for a trailing . in the hostname part of an email address. However, the trailing . is still valid hostname syntax.]
Following is the regular expression that is recommended by rfc2822 :
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
This regex is time-consuming therefore I believe that Zend went with a simpler regex which unintentionally ignores the trailing fullstop.
Helpful links:
http://www.regular-expressions.info/email.html
http://regexpal.com/
I'm a noob but trying vigorously to simply validate email addresses that only end in ".edu" or ".ac" is there a simple function/script/solution to this seemingly simple problem? able to use php,javascript or jquery.
Any help would be great thanks in advance!
You want a regular expression.
The following pattern tests for any e-mail address ending in a top-level domain like .com, .org, .net, .biz etc.
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b
You can use the preg_match function in PHP, pass this as the pattern, and just change the list of top-level domains you want to accept at the end. If the function returns 0, the address didn't validate, if it returns 1, it did match.
Regex source: http://www.regular-expressions.info/email.html
preg_match: http://php.net/manual/en/function.preg-match.php
jQuery also has a validate plugin with built-in patterns for validating e-mail addresses, but you'd need to combine this with the server-side validation in PHP for those people that have Javascript disabled.
Validate plugin: http://docs.jquery.com/Plugins/Validation/validate
if( strpos($_post['email'] , '.edu' ) == true){
....
}
NOTE: the 'name' in the input field of the email address should be 'email'
It is the most simple way out there to check if ".edu" is present in that email