我正在尝试验证保持在某个范围内的电话号码的长度。假设至少9个字符,但不超过12个,这样我就可以获得国际电话号码。
我尝试了几种方法,但都不起作用。
例如,下面的选项正确地验证了它没有字母,但是我引入的数字的长度并不重要,即使我引入了9、10或11个数字,我也总是收到错误消息:“您的电话号码需要有9-11个数字”。
非常感谢
if (empty($_POST["cellphone"])) {
$cellphoneErr = "Cell Phone is required";
} else {
$cellphone = test_input($_POST["cellphone"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[0-9]*$/",$cellphone)) {
$cellphoneErr = "Only numbers allow";
}
elseif(strlen($_POST["cellphone"] < 9) || strlen($_POST["cellphone"] > 11)){
$cellphoneErr = "Your phone number needs to have 9-11 numbers";
}
}
发布于 2016-03-22 04:55:53
对量词{min,max}
使用preg_match()
if (!preg_match("/^[0-9]{9,11}$/",$cellphone)) {
$cellphoneErr = "Has to be 9 to 11 numbers.";
}
发布于 2016-03-22 04:43:19
elseif(strlen($_POST["cellphone"] < 9) || strlen($_POST["cellphone"] > 11)){
应该是:
elseif(strlen($_POST["cellphone"]) < 9 || strlen($_POST["cellphone"]) > 11){
你的括号错了。
https://stackoverflow.com/questions/36141124
复制相似问题