Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用preg_match在PHP中进行表单验证_Php_Validation - Fatal编程技术网

使用preg_match在PHP中进行表单验证

使用preg_match在PHP中进行表单验证,php,validation,Php,Validation,嗨,我正在尝试验证一个简单的HTML表单。这是我的HTML代码- <HTML> <body> <form method="get" action="password.php"> User ID<input type="text" name="user" id="user" required/><br/> Password<input type="password" name=

嗨,我正在尝试验证一个简单的HTML表单。这是我的HTML代码-

<HTML>
    <body>
        <form method="get" action="password.php">
        User ID<input type="text" name="user" id="user" required/><br/>
        Password<input type="password" name="pass" id="pass" required/><br/>
        <input type="submit" value"Login Here">
        </form>
    </body>
    </html>

用户ID
密码
密码只能包含数字。我的password.php是-

<?php
    $name=$_GET['user'];
    $pass=$_GET['pass'];

    $strlen1= strlen($name);
    $strlen2= strlen($pass);

    $regex= "/^[789][0-9]{9}$/";   // my password should contain digits only.

    if($strlen1==0 | $strlen2==0)   
        {
            echo 'Username or password field is empty'.'<br/>';
        }

    else if($name==$pass)
        {
            echo 'Go for a better password'.'<br/>';
        }

    else if(!preg_match($regex,$pass))
        {
            echo 'password is invalid'.'<br/>';
        }

    else
        {
            echo 'You have successfully logged in'.'<br/>';
        }

    ?>

在运行脚本时,即使我输入了有效密码,如“123456”,消息也会显示“密码无效”。 请告诉我上面代码中的错误。
谢谢

您的密码应该以7、8、9中的任何一个开头,后面跟着9位数字

$regex= "/^[789][0-9]{9}$/";
要仅过滤数字,请将其替换为:

$regex="/^\d+$/";

您的正则表达式
^[789][0-9]{9}$
要求密码以
7
8
9
开头,后跟9位数字。将其更改为
^\d+$
以接受任意数量的数字,或至少接受6位数字,如
^\d{6,}$