• Resolved shibby15

    (@shibby15)


    Hello Support,

    I’m preparing a form for users registration in my website and I would like to validate the zipcode with this format 0000-000.

    How can I accomplish this?

    Thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter shibby15

    (@shibby15)

    Based on your example from here: https://docs.wpeverest.com/user-registration/docs/how-to-add-custom-validations-to-fields-programmatically/

    I’ve tested without success:

    add_action( ‘user_registration_validate_zipcode’,’ur_validate_zipcode_field’,10,4);
    function ur_validate_zipcode_field($single_form_field, $data, $filter_hook, $form_id) {
    $field_label = isset( $data->label ) ? $data->label : ”;
    $value = isset( $data->value ) ? $data->value : ”;
    if( 1 === preg_match(‘/\b\d{4}(-\d{3})?\b/’, $value)) {
    add_filter( $filter_hook, function ( $msg ) use ( $field_label ) {
    return __( $field_label . ‘Erro!’, ‘user-registration’ );
    });
    }
    }`

    Can you please help?

    Plugin Support Amrit Kumar Shrestha

    (@shresthauzwal)

    Hi @shibby15,

    Thank you for writing in,

    In the free version of the plugin, we do not have a specific field for Zipcode. If you are using the other input text field then the codes for validation do not work because the fields name does not save with “user_registration_validate_zipcode”.

    Could you please provide us your registration form link and mention it so that we can look into it and get back to you accordingly.

    Regards!

    Thread Starter shibby15

    (@shibby15)

    Hello @shresthauzwal

    I’m using the input text default and changed the field name to zipcode. I’ve also tried the example you shared using the field “text” and it worked.

    My registration form is available in this URL: https://wordpress-377373-1615575.cloudwaysapps.com/registo-de-utilizadores/

    There you can check the field text and also the zipcode.

    Thanks a lot for your help.

    Plugin Support Amrit Kumar Shrestha

    (@shresthauzwal)

    Hi @shibby15,

    Thank you for writing back,

    Could you please try the following code,

    add_action( 'user_registration_validate_text','ur_validate_text_field',10,4);
    function ur_validate_text_field($single_form_field, $data, $filter_hook, $form_id) {	
    	if( 'input_box_1627459889' === $data->field_name ) {
    	$field_label = isset( $data->label ) ? $data->label : '';
    	$value = isset( $data->value ) ? $data->value : '';
        $pattern = 'Place Your validation code here';
    	if( 1 === preg_match($pattern, $value ) ) {
    	    add_filter( $filter_hook, function ( $msg ) use ( $field_label ) {
    	        return __( $field_label . 'should not contain a  number.', 'user-registration' );
    	   });
    	}
    }
    }

    You need to replace the input_box_1627459889 with your field name.

    Regards!

    Thread Starter shibby15

    (@shibby15)

    Hello @shresthauzwal,

    Replaced field name zipcode to input_box_1627459889,

    Then updated functions.php of the childtheme with the code you shared but then I got broken website, so I removed the code. ??

    Thread Starter shibby15

    (@shibby15)

    Also, I tried to apply the pattern ‘/\b\d{4}(-\d{3})?\b/’to the field text, as the example code you shared worked:

    `add_action( ‘user_registration_validate_text’,’ur_validate_text_field’,10,4);
    function ur_validate_text_field($single_form_field, $data, $filter_hook, $form_id) {
    $field_label = isset( $data->label ) ? $data->label : ”;
    $value = isset( $data->value ) ? $data->value : ”;

    if( preg_match(‘/\b\d{4}(-\d{3})?\b/’, $value ) ) {
    add_filter( $filter_hook, function ( $msg ) use ( $field_label ) {
    return __( $field_label . ‘Testing this’, ‘user-registration’ );
    });
    }
    }

    This pattern should have restrited the user input to the format _ _ _ _ – _ _ _ but it didn’t worked.

    • This reply was modified 3 years, 8 months ago by shibby15.
    Plugin Support Amrit Kumar Shrestha

    (@shresthauzwal)

    Hi @shibby15,

    Sorry for the delay,

    We have well tested the code and provided the solution for you, it seems the preg_match code is not appropriate so that your added code is making your site down.

    Please use the following code to validate your input field with this format 0000-000.

    add_action( 'user_registration_validate_text','ur_validate_text_field',10,4);
    function ur_validate_text_field($single_form_field, $data, $filter_hook, $form_id) {
        if( 'input_box_1627459889' === $data->field_name ) {
            $field_label = isset( $data->label ) ? $data->label : '';
            $value = isset( $data->value ) ? $data->value : '';
            if ( !preg_match( '/^[0-9]{4}([- ]?[0-9]{3})?$/', $value ) ) {
                add_filter( $filter_hook, function ( $msg ) use ( $field_label ) {
                    return __( $field_label . 'should contain 0000-000 format zipcode.', 'user-registration' );
               });
            }
        }
    }

    You need to replace the input_box_1627459889 with your field name.

    Regards!

    Thread Starter shibby15

    (@shibby15)

    Thanks a lot for an awesome support! It worked like a charm…
    Appreciate your help!

    Can I replace the name “input_box_1627459889” for “zipcode”, making the adjustment in the code?

    Best regards!

    • This reply was modified 3 years, 7 months ago by shibby15.
    Thread Starter shibby15

    (@shibby15)

    Sorry, but it’s only working in the registration form, not in Profile Details ??

    Plugin Support Amrit Kumar Shrestha

    (@shresthauzwal)

    Hi there

    Thank you for writing back,

    Yes, you can change the “input_box_1627459889” with the “zipcode” field if your input field name is “zipcode”. For profile edit please use the following code

    add_action( 'user_registration_validate_user_registration_input_box_1627459889', 'ur_validate_text_field_in_profile_details', 10, 2 );
    function ur_validate_text_field_in_profile_details( $value ) {
     if ( !preg_match( '/^[0-9]{4}([- ]?[0-9]{3})?$/', $value ) ) {
     if ( 'yes' === get_option( 'user_registration_ajax_form_submission_on_edit_profile', 'no' ) ) {
     wp_send_json_error(
     array(
     'message' => __( 'should contain 0000-000 format zipcode.', 'user-registration' ),
     )
     );
     }else {
     ur_add_notice( __( 'should contain 0000-000 format zipcode.', 'user-registration' ), 'error' );
     }
     }
    }

    Similarly, you need to change input_box_1627459889 with your field name. (please use latest version of the user registration plugin to support this code on your edit profile)

    Regards!

    Thread Starter shibby15

    (@shibby15)

    Hello @shresthauzwal,

    Thank you for your support.

    About User Registration form – It is working but accepts the user registration when he puts only 4 digits, I mean when it only meets the first segment of the pregmatch. Users can enter only the first 4 digits or the correct pattern.

    About the Profile Details – not working here ?? I can share with you login data of a test user.

    Thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Zipcode Format input – I’m not using WooCommerce’ is closed to new replies.