错误消息未显示给用户

时间:2016-04-13 14:45:16

标签: php html

慢慢学习php还有很多东西要去。发现自己在书中的一个点我认为是有用的练习编码的例子。我想检查用户密码的长度和复杂程度。是的,我知道有更长的空间和更复杂的密码。这个问题足以让我慢慢学习。要求正好是9个字符和1个@符号。允许所有其他字符。当密码不符合要求时,不确定为什么不返回错误消息。任何帮助表示赞赏,并知道这可能是一匹被打败的死马。这里的大多数其他答案都比我想要的更复杂,但最终会到达那里。请建设性地评论......谢谢!

<?php
       $pwd = filter_input(INPUT_GET, 'password');
       $errmsg = "";

       //function with 2 parameters/one passed by reference
       function passVal($pwd) {
           $errmsg = null;
           if (!preg_match('/^(?=.*[@]){9}$/', $pwd)) {
                  $errmsg = "Password must contain exactly 9 characters and one @ sign.";
           }
           if (strlen($pwd == 9) && preg_match('/(?=.*[@])/', $pwd)) {
                 $errmsg = "Contains exactly 9 characters and there is at least one @ sign. Password is good";
           }
        return $errmsg;
      }
 ?>

HTML

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<header><h1>Password Check</h1></header>
<form action="" method="get">
<h3>Enter a password in the box.</h3><br>
<p>The password must be exactly 9 characters and include at least
one &#64 sign. All other characters are allowed.</p>
<p>Enter a password<input type="text" name="password"></p>
<p><button type="submit" formmethod="get" name="button">Check Password</button></p>
<p><?php echo $errmsg; ?></p>
</body>
</html> 

4 个答案:

答案 0 :(得分:3)

除了给出的其他答案。说明你从未调用过passVal()函数。

strlen()的条件声明让你失望。

if (strlen($pwd == 9) && preg_match('/(?=.*[@])/', $pwd))

,应该读作并将$pwd括在括号内:

if ((strlen($pwd) == 9) && preg_match('/(?=.*[@])/', $pwd))

根据strlen()函数上的手册http://php.net/manual/en/function.strlen.php

  • strlen($str);

所以你的(strlen($pwd == 9)将失败。

因此,您可以使用$pwd参数添加和回显函数:

echo passVal($pwd);

    return $errmsg;
}

然而,使用条件语句来检查它是否设置为空是更好。

旁注编辑: 您似乎没有关闭表单,因此如果这是您的实际代码,则需要添加</form>

玩弄想法编辑

您还可以使用三元运算符检查条件empty()并回显同一行内的函数。

即:<p><?php echo !empty($pwd) ? passVal($pwd) : ''; ?></p>并定义从$errmsg$msg_bad分配给$msg_good的两条不同消息。

这是一个完整的重写:

<?php

$pwd = filter_input(INPUT_GET, 'password');

$msg_bad = "<b>Password must contain exactly 9 characters and one @ sign.</b>";
$msg_good  = "Contains exactly 9 characters and there is at least one @ sign. Password is good";


//function with 2 parameters/one passed by reference
function passVal($pwd) {

global $msg_good, $msg_bad;

    $errmsg = null;


    if ((strlen($pwd) == 9) && preg_match('/(?=.*[@])/', $pwd)) {

        $errmsg = $msg_good;

    }

    else {

        $errmsg = $msg_bad;

    }

     return $errmsg;

}


?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<header><h1>Password Check</h1></header>

<form action="" method="get">
<h3>Enter a password in the box.</h3>
<p>The password must be exactly 9 characters and include at least
one &#64 sign. All other characters are allowed.</p>
<p>Enter a password<input type="text" name="password"></p>
<p><button type="submit" formmethod="get" name="button">Check Password</button></p>
</form>

<p><?php  echo !empty($pwd) ? passVal($pwd) : ''; ?></p>

</body>
</html>

答案 1 :(得分:0)

检查下面的代码,我将$ errmsg变量向下移动并调用该函数将返回值赋给它。

<?php
$pwd = $_GET['password'];

//function with 2 parameters/one passed by reference
function passVal($pwd) {
    $errmsg = null;

    if (!preg_match('/^(?=.*[@]){9}$/', $pwd)) {
        $errmsg = "Password must contain exactly 9 characters and one @ sign.";
        }

    if (strlen($pwd == 9) && preg_match('/(?=.*[@])/', $pwd)) {
         $errmsg = "Contains exactly 9 characters and there is at least one @ sign. Password is good";
}
    return $errmsg;
}

**$errmsg = passVal($pwd);**

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<header><h1>Password Check</h1></header>
<form action="" method="get">
<h3>Enter a password in the box.</h3><br>
<p>The password must be exactly 9 characters and include at least
one &#64 sign. All other characters are allowed.</p>
<p>Enter a password<input type="text" name="password"></p>
<p><button type="submit" formmethod="get" name="button">Check Password</button></p>
<p><?php echo $errmsg; ?></p>
</body>
</html> 

答案 2 :(得分:0)

据我所知,你永远不会调用passVal函数。你应该做这样的事情:

if( ! empty( $_GET ) ) {
    $errmsg = passVal( $_GET['password'] );
}

仅仅因为您提交表单而不会自动调用函数。实际上,您应该始终检查表单是否已提交,然后进行所需的处理。

理想情况下,您还应该将PHP逻辑与HTML分开,但这完全是另一个主题。

答案 3 :(得分:0)

你没有调用该函数而且不需要函数。这样做:

<?php
$pwd = filter_input(INPUT_GET, 'password');
$errmsg = "";
//function with 2 parameters/one passed by reference

if ((count($pwd)<9) && (strpos($pwd, "@") == false)) {
    $errmsg = "Password must contain exactly 9 characters and one @ sign.";
    }
else {
     $errmsg = "Contains exactly 9 characters and there is at least one @sign. Password is good";
}



?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<header><h1>Password Check</h1></header>
<form action="" method="get">
<h3>Enter a password in the box.</h3><br>
<p>The password must be exactly 9 characters and include at least
one &#64 sign. All other characters are allowed.</p>
<p>Enter a password<input type="text" name="password"></p>
<p><button type="submit" formmethod="get" name="button">Check   Password</button></p>
<p><?php echo $errmsg; ?></p>
</body>
</html>