[英]Simple 'Greater Than' form validation
I am trying to validate my registration form, and one of my validations doesn't work. 我正在尝试验证我的注册表,但其中一项验证无效。 I want to echo a error message if the first name is over 20 characters long. 如果名字超过20个字符,我想回显一条错误消息。 I am using the code 我正在使用代码
} else if($_POST["mem_first_name"] >20) {
$errors[] = 'Sorry but your First name is limited to 20 Characters each';
}
However no error is shown if more than 20 characters are entered. 但是,如果输入的字符数超过20个,则不会显示任何错误。 However if I use the same code but change it to less than like this 但是,如果我使用相同的代码,但是将其更改为小于这样的代码
} else if($_POST["mem_first_name"] <20) {
$errors[] = 'Sorry but your First name is limited to 20 Characters each';
}
Then it works, is there a simple fix? 然后工作了,有简单的解决方法吗?
if(strlen($_POST["mem_first_name"]) > 20)
You have to use strlen()
function for string length checks. 您必须使用strlen()
函数进行字符串长度检查。 Also, be aware that if you need multibyte encoding support, you should switch to using mb_strlen()
. 另外,请注意,如果需要多字节编码支持,则应切换为使用mb_strlen()
。
var_dump(strlen('bär')); // int(4) - byte length
var_dump(mb_strlen('bär', 'utf8')); // int(3) - character length
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.