• Resolved mikey520

    (@mikey520)


    Hi, I have a registration form with a mobile phone field, and I wish to add validation to check if the number already exists, I’ve read the documentation but still not so clear for my level, could you please advice how?

    Thanks a lot!
    Mike

    • This topic was modified 3 years, 4 months ago by mikey520.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @mikey520

    You can try the following code snippet:

    add_action("um_submit_form_register","um_090321_mobile_number_validation");
    function um_090321_mobile_number_validation( $post_form ){
      
        if(  isset( $post_form['mobile_number'] ) && ! empty( $post_form['mobile_number'] ) ){
            global $wpdb;
    
        $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$wpdb->usermeta} WHERE meta_key = 'mobile_number' AND meta_value = %s", $post_form['mobile_number'] ) );
            if( $wpdb->num_rows > 0 )  {
                UM()->form()->add_error('mobile_number', __( 'Mobile Number already registered', 'ultimate-member' ) );
    		}
    	}  
    
    }

    Ensure that the Mobile Number field has a meta key mobile_number in the Register form.

    You can add the code to your theme/child-theme’s functions.php file or use the Code Snippet plugin to run the code.

    Regards,

    Thread Starter mikey520

    (@mikey520)

    WOW, works like magic!!
    Thanks so much for your prompt and effective help. @champsupertramp

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @mikey520

    Thanks for letting us know.

    Regards,

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add custom validation for existing mobile number?’ is closed to new replies.