• Resolved sevenlenovo

    (@sevenlenovo)


    Hi there,

    Just wondering if I could get some assistance. I’m following the guide in the documentation on custom validation on fields but I can’t get this to work. I’m trying to implement validation on a password field (not the user_pass field for the user’s account password when registering, rather a second password field.) For example I want the field to only allow 9 digits, no letters and no symbols. Any idea on how to go about this?

    Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Sandip Pokhrel

    (@sandippokharel)

    Hi @sevenlenovo

    Please refer to the documentation in the link below.
    https://docs.wpuserregistration.com/docs/custom-code-snippets/#18-toc-title
    Does this help?

    Regards!

    Thread Starter sevenlenovo

    (@sevenlenovo)

    Oh I didn’t notice that page. Perfect, that worked. Thank you. I have another question I’m not sure if I should start a new topic.

    Is there a way to ensure that the text area field begins with a specific word? For example if I wanted to make sure the the first word entered into the text-area is ‘apple’ (or that it contains that word at all)

    Plugin Support Sandip Pokhrel

    (@sandippokharel)

    Hi @sevenlenovo

    Please use the following code.

    add_action('user_registration_validate_textarea', 'ur_validate_user_textarea_field', 10, 4);
    function ur_validate_user_textarea_field($single_form_field, $data, $filter_hook, $form_id) {
        $field_label = isset($data->label) ? $data->label : '';
        $textarea = isset($data->value) ? $data->value : '';
        
        // Split the username into words
        $textarea_words = explode(' ', $textarea);
        
        // Check if the first word is 'apple'
     if( 'textarea_1695289463' === $single_form_field->general_setting->field_name ) {
        if (isset($textarea_words[0]) && strtolower($textarea_words[0]) !== 'apple') {
            add_filter(
                $filter_hook,
                function ($msg) use ($field_label) {
                    return __('The first word of ' . $field_label . ' must be "apple".', 'user-registration');
                }
            );
        }
        }
    }

    Make sure you replace textarea_1695289463 with the field name of your textarea field.
    Please refer to the documentation in the link below for more details.
    https://docs.wpeverest.com/user-registration/docs/how-to-add-the-custom-code-snippet-on-your-site/
    This will help you to solve the issue. Please let us know if you have any other questions.

    Regards!

    Thread Starter sevenlenovo

    (@sevenlenovo)

    Great, thanks that worked. Somehow though it has made the field mandatory (even though it’s not mandatory in the form builder settings.) Tried setting it to mandatory and back to optional, no luck. When field is set to optional, no asterisk is shown on the registration form but it will still give an error message that the input is invalid.

    • This reply was modified 1 year, 5 months ago by sevenlenovo.
    Plugin Support Sandip Pokhrel

    (@sandippokharel)

    Hi @sevenlenovo

    Please try the following code.

    add_action('user_registration_validate_textarea', 'ur_validate_user_textarea_field', 10, 4);
    
    function ur_validate_user_textarea_field($single_form_field, $data, $filter_hook, $form_id) {
        $field_label = isset($data->label) ? $data->label : '';
        $textarea = isset($data->value) ? $data->value : '';
    
        // Check if the field is not empty
        if (!empty($textarea)) {
            // Split the textarea content into words
            $textarea_words = explode(' ', $textarea);
    
            // Check if the first word is 'apple'
            if ('textarea_1695289463' === $single_form_field->general_setting->field_name) {
                if (isset($textarea_words[0]) && strtolower($textarea_words[0]) !== 'apple') {
                    add_filter(
                        $filter_hook,
                        function ($msg) use ($field_label) {
                            return __('The first word of ' . $field_label . ' must be "apple".', 'user-registration');
                        }
                    );
                }
            }
        }
    }

    I hope this helps.

    Regards!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom Validation on Password Field’ is closed to new replies.