我如何验证这种数字

时间:2013-01-14 16:08:14

标签: php

我正在寻找能够验证10位长电话号码的功能或逻辑。我想确保数字正好是10位数字,没有字母,没有连字符等。还需要检查数字的第一位数字以0开头。

Eg: 0714556260

更新

$test = 0714556260;

if (preg_match("|^[0-9]{10}$|", $test)) {
      echo 'valid';
} else {
    echo 'not valid';
}

4 个答案:

答案 0 :(得分:1)

if (preg_match('/^0\d{9}$/', 0714556260) ) {
    // OK
}

答案 1 :(得分:1)

$str = "0714556260";
if (preg_match("|^[0-9]{10}$|", $str))
    echo "ok";

答案 2 :(得分:0)

您可以使用此正则表达式在preg_match函数中验证您的电话号码:

/^0\d{9}$/

答案 3 :(得分:0)

请阅读documentation

$test = 0714556260;

if (intval($test) && preg_match($test,"/^0[0-9]{9}$/")) {
      //do stuff
}