php preg_match正则表达式验证总是返回false

时间:2013-03-23 19:09:00

标签: php regex forms validation

请记住,如果没有preg_match的if语句验证有效,但是使用preg_match它总是返回false

我想验证该字段,并确保其中有8个数字,不多也不少。

function account_number_validation($validation_result){
  $form = $validation_result["form"];


 if(!preg_match('[0-9]{8}', $_POST['input_19'])){

    // set the form validation to false
    $validation_result["is_valid"] = false;

    //finding Field with ID of 1 and marking it as failed validation
    foreach($form["fields"] as &$field){

        if($field["id"] == "19"){
            $field["failed_validation"] = true;
            $field["validation_message"] = "This field is invalid!";
            break;
        }
    }

}

//Assign modified $form object back to the validation result
$validation_result["form"] = $form;
return $validation_result;

}

任何想法的人?

2 个答案:

答案 0 :(得分:0)

使用以下命令更改if语句:

if(!preg_match('/^[0-9]{8}$/', $_POST['input_19'])){

答案 1 :(得分:0)

试试这个:

// ...
if(!preg_match('#[0-9]{8}#', $_POST['input_19'])) {
// ...