Home >Backend Development >PHP Tutorial >php and javascript regular verification

php and javascript regular verification

WBOY
WBOYOriginal
2016-07-06 13:52:22947browse

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

Both front and backend are /^1[3|5|7|8d{8}$/ regular
Front-end verification has been used

Background execution return
Some people said on their website that the regular library of php is different from js
Is there any way to solve it

Reply content:

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

Both front and backend are /^1[3|5|7|8d{8}$/ regular
Front-end verification has been used

Background execution return
Some people said on their website that the regular library of php is different from js
Is there any way to solve it

I can’t stand it anymore...
These regular expressions on the Internet are really copied here and there. If one has a problem with the writing, then everyone has a problem. Why don’t you want to look at the regular expressions yourself
[0-9] and d are completely equivalent. Why should we separate them into [0-9]d{8}? Just write d{9} instead of

So change the regular expression to /^1[3|5|7|8]d{9}$/!
(The [ at the beginning of your code must have been copied too much or entered by mistake when copying)

As for your question, please refer to the following two pieces of code. The effects are completely equivalent:

PHP:

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

Javascript:

<code class="js">var re = /^1[3|5|7|8]\d{9}$/; 
var str = '13012345678';

if (re.test(str)) {
    console.log('验证通过');
} else {
    console.log('验证失败');
}</code>

<code>//假设前台有个表单的name是phone
$phone=$_POST['phone'];
 if ($phone != '[/^1[3|5|7|8][0-9]\d{8}$/'){
    return "PleaseEnterYourPhoneNumber";
}</code>

<code><?php
if (!preg_match('[/^1[3|5|7|8][0-9]\d{8}$/', $phone))
{
    return "PleaseEnterYourPhoneNumber";
}
?></code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:symfony third-party loginNext article:symfony third-party login