Php and javascript regular expression verification-php Tutorial

Source: Internet
Author: User
Tags php regular expression
{Code ...} the frontend and backend are all ^ 1 [3 | 5 | 7 | 8d {8} $ regular. frontend verification has been performed on the backend and return is used. some people say that they say on the Internet that php's regular library is different from js. how can this problem be solved? solve the problem
if (!preg_match('[/^1[3|5|7|8][0-9]\d{8}$/', $phone)){    return "PleaseEnterYourPhoneNumber";}

Both front and back ends are/^ 1 [3 | 5 | 7 | 8d {8} $/regular
Used for front-end verification

Return in the background
Some people say that the php regular expression library is different from the js Library on the Internet.
Is there any solution?

Reply content:
if (!preg_match('[/^1[3|5|7|8][0-9]\d{8}$/', $phone)){    return "PleaseEnterYourPhoneNumber";}

Both front and back ends are/^ 1 [3 | 5 | 7 | 8d {8} $/regular
Used for front-end verification

Return in the background
Some people say that the php regular expression library is different from the js Library on the Internet.
Is there any solution?

Can't bear it...
This type of regular expression on the Internet is actually copied. if there is a problem with writing, all of them have problems. why don't you choose to read the regular expression on your own?
[0-9]And\dIt is completely equivalent. why do we need to split it[0-9]\d{8}, Directly write\d{9}No.

Therefore, the regular expression is changed/^1[3|5|7|8]\d{9}$/Right!
(The one at the beginning of your code[It must have been copied more or mistakenly input)

As for your problem, refer to the following two sections of code, which are equivalent in effect:

PHP:

$ Re = '/^ 1 [3 | 5 | 7 | 8] \ d {9} $/'; $ str = '000000'; if (preg_match ($ re, $ str) {echo 'verification passed ';} else {echo 'verification failed ';}

Javascript:

Var re =/^ 1 [3 | 5 | 7 | 8] \ d {9} $/; var str = '123'; if (re. test (str) {console. log ('verified pass');} else {console. log ('verification failed ');}

// Assume that the name of a form in the foreground is phone $ phone =$ _ POST ['phone']; if ($ phone! = '[/^ 1 [3 | 5 | 7 | 8] [0-9] \ d {8 }$/') {return "PleaseEnterYourPhoneNumber ";}


  

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.