Why does the preg_match function check all fields with a specific field argument?

I have a form in which I am using a function preg_match

to validate fields. I have a generic function for mapping. The function validateForm()

is called earlier in the script with the appropriate values.

When the function does NOT pass any values, all fields display an error message despite matching the information correctly. Generalized function with no arguments:

    function validateForm() {

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

      

When I only pass one specific argument of a regex / field pair, all the fields start validating and show an error message when needed (so basically the code works as it should, despite having a field specific argument in that function ). For example, when I pass this single regex / field argument in preg_match

, all fields start validating each field correctly, regardless of the fact that in this case I only validate the City field. An 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 a specific argument is passed for a specific field, the function will work for all individual arguments preg_match

in the code? The script works the way I would like it to, I just don't understand why a particular argument is what makes it validate all fields.

Here's all the PHP code:

<?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 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

?>

      

+3


source to share


1 answer


You still need to have arguments for your function. The code below will activate your validation function.

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

      



I also see other potential problems not checking if post variables are set before using them and you set $ show_form = true for all if / else cases. I'm sure you can figure out the rest with some debugging instructions.

0


source







All Articles