This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

Validate Zip Code Field


  1. mindseye
    Member

    I would like to validate the zipcode field in my forms to check for a minimum of 5 characters. Currently people are adding state abbreviations in the zipcode and not getting an error message.

    how can I specify the validation for that field only?

    Thanks.

    Posted 15 years ago #
  2. redityo

    Hi,

    Try to edit "includes/post-functions.php" file and go around line 479 ~ 481, you'll see these code :

    if($validation_result !== true){
    	$error_elements[$element_id] = $validation_result;
    }

    then put these code exactly bellow that line

    if ($form_id == 5) {
    	if (strlen(trim($target_input[$element_name_5])) < 5 && empty($error_elements[$element_id]) ) {
    		$error_elements[$element_id] = "Zip code must more than 5 Character";
    	}
    }

    Those code will work on form id 5, make sure you change the form id with yours


    MachForm Support

    Posted 15 years ago #
  3. mindseye
    Member

    Very nice. Thank you for your help.

    Posted 15 years ago #
  4. mindseye
    Member

    Now people are entering the state name for example "Florida" and since there are more than 5 characters the form is accepting this entry.

    I added this to the code || !preg_match("/^[0-9][\w]*$/i",$value[5])

    Would this work?

    if ($form_id == 4 || $form_id == 10) {
    if (strlen(trim($target_input[$element_name_5])) < 5 && empty($error_elements[$element_id]) || !preg_match("/^[0-9][\w]*$/i",$value[5]) ) {
    $error_elements[$element_id] = "Zip code must be more than 5 Characters";
    }
    }

    Posted 15 years ago #
  5. redityo

    If you need to allow numeric value only for "ZIP" code, try to use this code instead :

    if ((strlen(trim($target_input[$element_name_5])) < 5 || !is_numeric($target_input[$element_name_5])) && empty($error_elements[$element_id]) ) {
    	$error_elements[$element_id] = "Zip code must more than 5 Character";
    }

    I think it's more simple to use "is_numeric" function


    MachForm Support

    Posted 15 years ago #
  6. mindseye
    Member

    Thanks redityo. I appreciate your help. The form works great now.

    Posted 15 years ago #

RSS feed for this topic

Reply