preg_match can not find the "Dash" in an email - php

I am using the following FUNCTION to extract email address from text.
function is_valid_email($email) {
if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/',$emailss)) return true;
else return false;
}
It is working very smoothly, but on problem:
an email with "dash" is not working:
for example:
info-test#web-site.com comes out: test#web
Please advise.

Dash has special meaning in regular expression. So cant be used directly and need to be escaped using backslash. following is updated code:
function is_valid_email($email) {
if (preg_match('/^[_a-z0-9\-]+(\.[_a-z0-9\-]+)*#[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.([a-z]){2,4})$/',$emailss)) return true;
else return false;
}

You should escape the dash character, as it has a special meaning (range) in the used context:
[_a-z0-9\-]

There are myriads of problems with that e-mail validation regexp. For example, it won't pass any of perfectly valid modern national TLDs and it honestly thinks that TLD has maximum 4 letters in it. It doesn't allow arbitrary number of dots . in user account part, it doesn't allow pluses +, etc.
Generally, a good practice of validating e-mails boils down to:
Minimal validation - just check that there's # there and that's all.
Just send that e-mail - don't check anything else. If it will be sent - then it's indeed a valid e-mail.
For more details, take a look at http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/ or any similar articles.

Related

is this right way to validates an email address in the form [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
1st post here so bear with me lol
just wanted to check my work here i have a form that validates a email address but with the change to php 5.3 now errors
can some one please look over my change and tell me what im missing as it not working
OLD WAY
function valid_email($email)
{
// check an email address is valid
if (ereg('^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+#([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$', $email))
return true;
else
return false;
}
NEW WAY
function valid_email($email)
{
// check an email address is valid
if (preg_match(('^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+#([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$', $email))
return true;
else
return false;
}
form sends with no email validation.
i no its prob some thing simple but i cant work it out
thanks in advance
Do not use regular expressions for validating email addresses or hyperlinks. Also, as far as I know, ereg has been deprecated. Use preg from now on with regular expressions. The code for validating email addresses is:
if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
echo "This email address: $email_address is invalid. Enter a new one please";
}
If you must use a regular expression, use this RFC-822 regex
, but that may be a bit too complicated for what you're trying to do.
First unlike ereg, with preg_match you need to enclose the pattern within a delimiter, which is usually "/" though you can use other characters too if you want. For example,
preg_match('/^pattern$/', $email)
Secondly in your code there is an extra "(" after the function name "preg_match((" which should be "preg_match(" instead.
You might want to consider using the following regular expressions. It prevents domain name starting or ending with a dash such as username#-domain-.com but still allow dash within domain name such as username#my-domain.com
function isValidEmail($email)
{
return preg_match('/\A[a-z0-9]+([-._][a-z0-9]+)*#([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,4}\z/', $email)
&& preg_match('/^(?=.{1,64}#.{4,64}$)(?=.{6,100}$).*/', $email);
}
See validate email address using regular expression in PHP.

preg_match for the email validation I want but somehow I don't know where I messed up

oh eh...ya...lots commented there are lots email validation can be used but just that for this one I have to do it like what is mentioned below that's why....
I need to validate email like this
alphanumeric characters followed by # followed by alphanumeric characters followed by . followed by 2 – 4 more alphanumeric characters
this is what I have done but somehow I know it's the last part after . I messed up but I couldn't find where I messed up....
preg_match("/^([0-9]|[a-z])([0-9]|[a-z]|[_-])*#([0-9]|[a-z])*\.([0-9][a-z]){2,4}$/i","")
at start I used [0-9]|[a-z])([0-9]|[a-z]|[_-] because I didn't want people able to use _- as the start....so forced start as number/letters only
There must be a million different people that wrote a new regex for email validation. If you are interested in the email format you can just use
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
and if the final value is empty the initial one wasn't a valid email address format.
(as an extra step you could try to validate the domain by using this function http://php.net/manual/en/function.checkdnsrr.php)
Have a try with this:
^[0-9a-z_\-]+#[0-9a-z_\-]+\.[0-9a-z]{2,4}$
But as said: there are ready-to-use regexes, much better than trying to reinvent the wheel. Also this current approach does not macth all valid addresses and validates some addresses that are illegal.
Which reason of email validation? It is very upset when you try to enter you email and you can't due to the stupid validation. I think it is enoth to check the availability of '#' and '.' signs, in case user unintentionally missed this.
$res = preg_match("/#[^#\.]*\./", $str);

PHP custom regex

I've written this regex to check for valid emails: /^[-a-z0-9._]+#[-a-z0-9._]+\.+[a-z]{2,6}$/i
I want it to work for emails like name1+name2#domaine.com
How can I fix this regex?
I Have a simpler solution.
if(filter_var($email,FILTER_VALID_EMAIL))
{
//true
}
this would be sufficient in most cases, this actually runs an regular check in C which in turn would be faster but if you wish to have control over the reg-ex in your application then the regex below is what's used for this check:
/^((\\\"[^\\\"\\f\\n\\r\\t\\b]+\\\")|([\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\~\\/\\^\\`\\|\\{\\}\\=\\?]+(\\.[\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\~\\/\\^\\`\\|\\{\\}\\=\\?]+)*))#((\\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\\-])+\\.)+[A-Za-z\\-]+))$/D
Another tip i will give you is that a user may enter an email address such as: invalid#dontexists.com which would then bypass your checks for a valid email, if you wan't to make sure that dontexists.com is running an email server is do:
$has_mx_server = (bool)checkdnsrr($domain,"MX");
if the domain has a registered MX Record the chances of the email being faked is reduced by a good chunk.
First part
[-a-z0-9._]+
does not accept right now plus sign. Expand it:
[-+a-z0-9._]+
Try
/^[-a-z0-9._+]+#[-a-z0-9._]+\.+[a-z]{2,6}$/i
Place the + inside the braces and escape it with a backslash
/^[-a-z0-9._\+]+#[-a-z0-9._]+\.+[a-z]{2,6}$/i
"+" is a meta character meaning to search for 1 or more occurrence, therefore, to search for the actual character, it must be escaped.

PHP server-side validation regular expression match

I have the following part of a validation script:
$invalidEmailError .= "<br/>» You did not enter a valid E-mail address";
$match = "/\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/";
That's the expression, here is the validation:
if ( !(preg_match($match,$email)) ) {
$errors .= $invalidEmailError; // checks validity of email
}
I think that's enough info, let me know if more is needed.
Basically, what happens is the message "You did not enter a valid E-mail address" gets echoed no matter what. Whether a correct email address or an incorrect email address is entered.
Does anyone have any idea or a clue as to why?
EDIT: I'm running this on localhost (using Apache), could that be the reason as to why the preg_match ain't working?
Thanks!
Amit
Your regex only includes [A-Z], not [a-z]. Try
$match = "/\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i";
to make the regex case-insensitive.
You can test this live on http://regexpal.com.
However, I'd advise you to try one of the expressions on the page mentioned by strager: http://fightingforalostcause.net/misc/2006/compare-email-regex.php. They have been perfected over time and will probably behave better. But Gmail users will be satisfied with yours, since they'll be able to use plus aliases which are rejected incorrectly by many validators.
You likely got the regular expression you're using from regular-expressions.info. On that page, the author states (emphasis added):
If you want to use the regular expression above, there's two things you need to understand. First, long regexes make it difficult to nicely format paragraphs. So I didn't include a-z in any of the three character classes. This regex is intended to be used with your regex engine's "case insensitive" option turned on. (You'd be surprised how many "bug" reports I get about that.) Second, the above regex is delimited with word boundaries, which makes it suitable for extracting email addresses from files or larger blocks of text. If you want to check whether the user typed in a valid email address, replace the word boundaries with start-of-string and end-of-string anchors, like this: ^[A-Z0-9._%+-]+#[A-Z0-9.-]+.[A-Z]{2,4}$.
To solve this problem, add the i PCRE flag after your regular expression.
You can always try debugging your regex using a simpler tool (I'm quite fond of using Notepad++ for this purpose) and performing iterative tests - ie. making the expression more/less complicated and seeing if that fixes/breaks things.

PHP expressions fail when changing from ereg to preg_match

I have a class that uses PHP's ereg() which is deprecated.
Looking on PHP.net I thought I could just get away and change to preg_match()
But I get errors with the regular expressions or they fail!!
Here are two examples:
function input_login() {
if (ereg("[^-_#\.A-Za-z0-9]", $input_value)) { // WORKS
// if (preg_match("[^-_#\.A-Za-z0-9]", $input_value)) { // FAILS
echo "is NOT alphanumeric with characters - _ # . allowed";
}
}
// validate email
function validate_email($email) {
// return eregi("^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email); // FAILS
}
You forgot the delimiters:
if (preg_match("/[^-_#.A-Za-z0-9]/", $input_value))
Also, the dot doesn't need to be escaped inside a character class.
For your validation function, you need to make the regex case-insensitive by using the i modifier:
return preg_match('/^[_.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i', $email)
I can't suppress the suspicion anymore that people simply don't like my +#example.org email address (I changed the right part only). It is an absolutely normal address, it's valid, short and easy to type. Simply people don't like it! One cannot register on any page using that mail.
So, why don't be nice and use PHPs Filter extension to verify the mail or to use a PCRE, which definitely allows all valid emails in use (excluding only the #[] ones):
/^[^#]+#(?:[^.]+\.)+[A-Za-z]{2,6}$/
Thanks for saving my email, it's a dieing species!
try preg_match("/[^-_#\.A-Za-z0-9]/", $input_value)

Categories