• Resolved enkoes

    (@enkoes)


    Hi, I have a field in Registration form called “NRIC Number” where users need to key in a 12-digit IC number of their own. In my case, I don’t want user to register more than one username using the same IC number. How can I create a custom validation for number field so that each IC number registered must be unique and cannot be the same?

    Regards.

Viewing 6 replies - 1 through 6 (of 6 total)
  • @enkoes

    You can try to use the “Unique Metakey value” UM builtin validation.

    Thread Starter enkoes

    (@enkoes)

    Thanks for your reply.

    I changed the validate from “Numeric value only” to “Unique metakey value” for NRIC number field in my both registration and profile forms. When I tested it by registering new user with one of member’s NRIC no, there wasn’t any error message prompted. Something missing?

    Regards.

    @enkoes

    Something missing?

    Yes, UM must test for the UM Mode ie if it’s during Registration or a Profile update.

    I have made an UM Bug report about your issue.

    https://github.com/ultimatemember/ultimatemember/issues/1418

    Thread Starter enkoes

    (@enkoes)

    @missveronicatv Thanks for your help! Hope it will be fixed in the next plugin update.

    @enkoes

    You can try this code snippet until fixed by the UM developers.

    For the Registration form set Custom Validation of the NRIC Number field
    and set Custom Action to: custom_unique_value_NRIC_Number

    add_action( "um_custom_field_validation_custom_unique_value_NRIC_Number", 'custom_unique_value_NRIC_Number', 10, 3 );
    function custom_unique_value_NRIC_Number( $key, $array, $submitted_data ) {
    
        if ( $submitted_data[ $key ] != '' ) {
    
            $args_unique_meta = array(
                'meta_key'      => $key,
                'meta_value'    => $submitted_data[ $key ],
                'compare'       => '=',
            );
    
            $meta_key_exists = get_users( $args_unique_meta );
    
            if ( $meta_key_exists ) {
                UM()->form()->add_error( $key , __( 'You must provide a unique value', 'ultimate-member' ) );
            }
        }
    }

    Install the code snippet into your active theme’s functions.php file
    or use the “Code Snippets” plugin.

    https://www.ads-software.com/plugins/code-snippets/

    Thread Starter enkoes

    (@enkoes)

    Thanks a lot. It works perfectly!

    I will keep this code running until UM fix the “Unique Metakey value” issue.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom validation for unique number’ is closed to new replies.