我有一个表单,但是我很难让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";
} */发布于 2016-06-16 17:53:22
这就是它搞砸的地方:
if (strlen( ($_POST['frmPassword1']) < 7 ) {让我们重新开始这个陈述吧。
首先,需要由表单字段frmPassword1表示的字符串:
$_POST['frmPassword1']然后需要字符串长度:
strlen($_POST['frmPassword1'])然后,您希望将其与小于8个进行比较,因为您专门要求的字符超过7个。因此,你的表达方式是:
strlen($_POST['frmPassword1']) < 8现在,让这成为一个完整的条件,如:
if( strlen($_POST['frmPassword1']) < 8 ){
//insert relevant code here telling users password is too short
}现在,您有了一组工作代码。
发布于 2016-06-16 17:41:27
看看你的():
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)YOu需要:
if (strlen($_POST['frmPassword1']) < 7) {
a b b a注意()上的标签。
发布于 2016-06-16 17:45:07
你错过了end )
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个字符的。所以使用
<=(小于或等于)。
https://stackoverflow.com/questions/37865924
复制相似问题