2016-03-21 11 views
0

範囲内に収まる電話番号の長さを検証しようとしています。 9文字以上12文字以下とし、国際電話番号を取得できるとします。PHPで携帯電話の長さを確認する

私はいくつかのことを試みましたが、どれも動作しませんでした。

以下のオプションは、文字がないことを正しく検証しますが、私が紹介した数字の長さに関係なく、私はいつもエラーメッセージ "あなたの電話番号は9-11の数字が必要です"たとえ9桁、10桁、または11桁の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"; 
} 
} 
+0

いくつかの国は、以下の9桁の数字を使用します –

答えて

0

elseif(strlen($_POST["cellphone"] < 9) || strlen($_POST["cellphone"] > 11)){

ありがとうございことが必要です。

elseif(strlen($_POST["cellphone"]) < 9 || strlen($_POST["cellphone"]) > 11){

あなたの括弧は間違っています。

1

数量詞{min,max}で使用preg_match()

if (!preg_match("/^[0-9]{9,11}$/",$cellphone)) { 
    $cellphoneErr = "Has to be 9 to 11 numbers."; 
}