PHP and JavaScript Regular validation

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

Both front and rear stations are/^1[3|5|7|8d{8}$/regular
Front desk verification used

Background execution return
Some people say that their web-based PHP regular library and JS are not the same
Is there any way to solve this?

Reply content:

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

Both front and rear stations are/^1[3|5|7|8d{8}$/regular
Front desk verification used

Background execution return
Some people say that their web-based PHP regular library and JS are not the same
Is there any way to solve this?

Can't bear it ...
Online This kind of regular is really is copied to copy, a write has a problem on all have problems, why refused to see the regular expression
[0-9]And \d is exactly equivalent ah, why to separate into [0-9]\d{8} , directly write a \d{9} not good

So just change /^1[3|5|7|8]\d{9}$/ it!
(The first one in your code [ must be copied or mistakenly typed)

As for your question, refer to the following two-paragraph code, which is completely equivalent in effect:

Php:

$re = '/^1[3|5|7|8]\d{9}$/'; $str = '13012345678';  if (preg_match($re, $str)) {    echo '验证通过';} else {    echo '验证失败';}

Javascript:

var re = /^1[3|5|7|8]\d{9}$/; var str = '13012345678';if (re.test(str)) {    console.log('验证通过');} else {    console.log('验证失败');}

//假设前台有个表单的name是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.