preg_match只允许10位数字,没有特殊字符或空格

时间:2013-08-08 09:28:48

标签: regex

我有以下功能:preg_match('/^[0-9]{10}$/'只允许10位数。如果我输入041 123456它接受它。我该如何防止空间?我希望用户仅输入电话号码041123456。不得允许任何空格或特殊字符。

我正在使用的代码是

If( ($fax_1_length != 0) | preg_match('/^[0-9]{10}$/', $fax_1) ){
    $fax_1_lenght_valid = true;
    $fax_1 = mysql_real_escape_string(stripslashes($_POST['fax_1']));   
    }
    else
    {
            $mistakes[] = 'ERROR - Your 1st Fax Number should only contain numbers or is empty.';
    }

1 个答案:

答案 0 :(得分:2)

问题在于这一行:

if( ($fax_1_length != 0) | preg_match('/^[0-9]{10}$/', $fax_1) )

|用于按位ORing。

您应该使用&&代替|

if( ($fax_1_length > 0) && preg_match('/^[0-9]{10}$/', $fax_1) )

如果长度为>则阻止执行0&&电话号码仅包含10位数字。

相关问题