Php regular expression email address

Source: Internet
Author: User
Tags php regular expression regular expression

There is a php regular expression mailbox problem

The code is as follows: Copy code

$ A = '/([w. _] {2, 10}) @ (w {1,}). ([a-z] {2, 4 })/';
$ B = '1412316545645454545454545k @ qq.com ';

If (preg_match ($ a, $ B )){
Echo "the email is valid ";
} Else {
Echo "illegal email ";
}

Why is the above output valid ??? {} Isn't it only allowed to put 2 to 10 digits here? It already exceeded 10 digits

This regular expression can be matched, matching this part '545454545k @ qq.com'
The first part of the 1412424545645454 does not match, so it is still valid.
If you want to match the entire segment correctly, you can change it to this

The code is as follows: Copy code
$ A = '/^ ([w. _] {2, 10}) @ (w {1,}). ([a-z] {2, 4}) $ /';


Pay attention to your regular expression

The code is as follows: Copy code
/([W. _] {2, 10}) @ (w {1,}). ([a-z] {2, 4 })/

He has no assertions, meaning that the string matches as long as there are matching parts
Therefore, {} can only match 2 to 10 characters, but does not match the entire string.

You must use assertions to determine whether a full string is matched. For example, ^ $

The code is as follows: Copy code
/^ ([W. _] {2, 10}) @ (w {1,}). ([a-z] {2, 4}) $/

Note that the header and tail are added with ^ $

 

The code is as follows: Copy code
$ A = '/^ ([w. _] {2, 10}) @ (w {1,}). ([a-z] {2, 4}) $ /';
$ B = '1412316545645454545454545k @ qq.com ';
If (preg_match ($ a, $ B )){
Echo "the email is valid ";
} Else {
Echo "illegal email ";
}

Now let's make better writing.

PHP mail verification regular expression newbie instance:

The code is as follows: Copy code

<? Php
Function isEmail ($ email ){
If (preg_match ("/^ [0-9a-zA-Z] + @ ([0-9a-zA-Z] +) [.]) + [a-z] {2, 4} $/I ", $ email )){
Return 'yesmail ';
} Else {
Return 'not mailbox ';
}
}
?>

Regular expression for Javascript (js) email verification:

Myreg =/^ ([a-zA-Z0-9] + [_ |.]?) * [A-zA-Z0-9] + @ ([a-zA-Z0-9] + [_ |.]?) * [A-zA-Z0-9] +. [a-zA-Z] {2, 4} $ /;
This can be verified in the form of a mailbox such as: I @julying.com, I @ I .com
Javascript (js) email address verification regular expression newbie instance:
<Script type = "text/javascript">
Function isEmail (val ){
Var myreg =/^ ([a-zA-Z0-9] + [_ |.]?) * [A-zA-Z0-9] + @ ([a-zA-Z0-9] + [_ |.]?) * [A-zA-Z0-9] +. [a-zA-Z] {2, 4} $ /;
 
If (! Myreg. test (val ))
Return 'not mailbox ';
Return 'yesmail ';
};
Alert (isEmail ('I @ julying.com '));
</Script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.