如何设置密码> 7个字符(验证)?

时间:2016-06-16 17:37:40

标签: php forms passwords

我有一个表单,但是我无法使strlen函数正常工作。 下面是代码示例 - 进一步验证。 我已经注释掉了无效的代码。基本上,我想用这段代码做的就是确定密码匹配,并且长度超过7个字符。

有人可以帮忙吗?

if (isset($_POST['formName']) && $_POST['formName'] == "addUser") {

if ( ( $_POST['frmName'] != '') &&
     ($_POST['frmSurname'] != '') &&
     ($_POST['frmEmail'] != '') &&
     ($_POST['frmPassword1'] != '') ) {


    if ($_POST['frmPassword1'] != $_POST['frmPassword2'] )  {

        echo "Passwords do not match!";
    } 

/*  if (strlen( ($_POST['frmPassword1']) < 7 ) {

        echo "Passwords much be a minimum of 7 characters"; 
    } */

3 个答案:

答案 0 :(得分:2)

看看你的():

strlen( ($_POST['frmPassword1']) < 7 )
      a b                      b     a
      ^-----strlen-------------------^

你没有测试$ _POST值的长度,你在foo < 7的布尔结果上做strlen,它总是0/1:

php > var_dump(strlen(true), strlen(false));
int(1)
int(0)

你需要:

if (strlen($_POST['frmPassword1']) < 7) {
   a      b                      b    a

请注意()上的标签。

答案 1 :(得分:0)

您缺少结束)

if (strlen( ($_POST['frmPassword1']) < 7 ) {
   1      2  3                     3     2  # 1 is missing

所以它会是

if (strlen( ($_POST['frmPassword1']) < 7 ) ){
   1      2  3                     3     2 1
  
    
      

注意:在您的问题中,您提到密码匹配且超过7个字符。因此,请使用<=(小于或等于)。

    
  

答案 2 :(得分:0)

This is where its messed up:

if (strlen( ($_POST['frmPassword1']) < 7 ) {

Let's start that statement over.

First you want the string represented by form field frmPassword1:

$_POST['frmPassword1']

Then you want the string length:

strlen($_POST['frmPassword1'])

Then you want to compare it to less than 8 because you specifically asked for more than 7 characters. Therefore, your expression would be:

strlen($_POST['frmPassword1']) < 8

Now make that a complete condition like so:

if( strlen($_POST['frmPassword1']) < 8 ){
 //insert relevant code here telling users password is too short
}

Now you have a working block of code.