douhui4699 2015-06-06 18:13
浏览 49

为什么preg_match函数验证具有特定字段参数的所有字段?

I have a form in which I am using a preg_match function to validate fields. I have a generalized function for the matching. The function validateForm() is being called earlier on in the script with the appropriate values.

When the function is NOT passed any values, all the fields show the error message despite having correctly matching information. Generalized function with no arguments:

    function validateForm() {

    if(preg_match()) {
        return true;
    }
    else {
        return false;
    }
} //  end function validateForm

When I pass just ONE specific regex/field pair argument, all the fields begin to validate and show the error message when appropriate (so basically the code works as it should despite having a field-specific argument in the function). For example, when I pass this single regex/field argument into preg_match, all the fields begin to validate each field correctly, regardless of the fact that I am only checking for the 'City' field in this case. Example of passing a field-specific argument, in which all the code 'works':

    function validateForm($cityRegex, $city) {

    if(preg_match($cityRegex, $city)) {
        return true;
    }
    else {
        return false;
    }
} //  end function validateForm

Can someone explain to me why, when passed a specific argument for a specific field, the function will work for all individual preg_match arguments in the code? The script is running as I would want it to, I just do not understand why the specific argument is what makes it validate all fields.

Here is all of the PHP code, if needed:

<?php
    $first = '';
    $last = '';
    $phone = '';
    $city = '';
    $state = ''; 
    $error_message = '';

    $firstLastRegex = '/^[a-zA-Z]{2,15}$/';
    $lastRegex = '/^[a-zA-Z]{2,15}$/';
    $phoneRegex = '/^(\(\d{3}\))(\d{3}\-)(\d{4})$/';
    $cityRegex = '/^[a-zA-Z]{3,20}$/';
    $stateRegex = '/^[a-zA-Z]{2}$/';

    $validate_first = '';
    $validate_last = '';
    $validate_phone = '';
    $validate_city = '';
    $validate_state = '';

    $phone_string = '';



    if(isset($_POST['submit'])) {

        $first = $_POST['firstName'];
        $last = $_POST['lastName'];
        $phone = $_POST['phoneNumber'];
        $city = $_POST['userCity'];
        $state = $_POST['userState']; 

        $show_form = false;

        $phone_string = str_replace(array('-', '(', ')'), '', $phone);

        $validate_first = validateForm($firstLastRegex, $first);
        $validate_last = validateForm($lastRegex, $last);
        $validate_phone = validateForm($phoneRegex, $phone);
        $validate_city = validateForm($cityRegex, $city);
        $validate_state = validateForm($stateRegex, $state);


        if($validate_first == false) {
                $show_form = true;
                $error_message .= "Please enter your FIRST name between 2 and 15 letters.<br>";
        }

        if($validate_last == false) {
            $show_form = true;
            $error_message .= "Please enter your LAST name between 2 and 15 letters.<br>";
        }

        if($validate_phone == false) {
            $show_form = true;
            $error_message .= "Please enter your phone number in (###)###-### format.<br>";
        }

        if($validate_city == false) {
            $show_form = true;
            $error_message .= "Please enter your city name between 3 and 20 letters.<br>";
        }

        if($validate_state == false) {
            $show_form = true;
            $error_message .= "Please enter your state's abbreviation (Example: CA).<br>";
        }

    } // end if isset();

    else {
        $show_form = true;
        $error_message = "";
    } // end else


    // REGEX FUNCTION

        function validateForm() {

        if(preg_match()) {
            return true;
        }
        else {
            return false;
        }
    } //  end function validateForm

?>
  • 写回答

1条回答 默认 最新

  • dongqing7789 2015-06-12 00:26
    关注

    You still need to have arguments for you function. The code below will make your validate function work.

        function validateForm($regEx, $field) {
        if(preg_match($regEx, $field)) {
            return true;
        }
        else {
            return false;
        }
    } //  end function validateForm
    

    I also see other potential issues with not checking if post variables are set before using them, and you are setting $show_form = true for all your if/else cases. I'm sure you can figure everything else out with some debug statements.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭