• Resolved sircoca

    (@sircoca)


    I need a numeric field on my registration form to be unique, for example, if a user puts the number 1 in this field, another user gets an error message when trying to put the same number in this field, is it possible?

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

    (@sandippokharel)

    Hi @sircoca

    Thanks for writing in,

    As per the current version of the plugin, this feature is not available. However, we have added this to our request list and if feasible, we will work on this feature. Please let us know if you have any other questions.

    Regards!
    WPEverest Support

    Thread Starter sircoca

    (@sircoca)

    I found a way to do it with the function that I will leave below

    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) {    
    	global $wpdb;
        $field_label = isset( $data->label ) ? $data->label : '';
        $value = isset( $data->value ) ? $data->value : '';
        if( 'YOUR_FIELD_NAME' === $single_form_field->general_setting->field_name ) {
    		if(1 != empty($value)) {
    			$res = $wpdb->get_var("SELECT * from wp_users us join wp_usermeta um on um.user_id = us.ID 
    					where um.meta_key = 'user_registration_YOUR_FIELD_NAME' and um.meta_value = $value");
    				if( count($res) > 0 ) {
    					add_filter( $filter_hook, function ( $msg ) use ( $field_label ) {
    						return __( $field_label . 'There is already a user with this number registered.', 'user-registration' );
    				   });
    				}
      		 }
        }
    
    if( 'YOUR_FIELD_NAME' === $single_form_field->general_setting->field_name ) {
    		if(1 != empty($value)) { 
    			if( 1 != ctype_digit($value) ) {
    				add_filter( $filter_hook, function ( $msg ) use ( $field_label ) {
    					return __( $field_label . 'The number field can only contain numbers.', 'user-registration' );
    			   });
    			}
    		}
    	}
    }
    Plugin Support Sandip Pokhrel

    (@sandippokharel)

    Hi there,

    Yes, this is the correct way to do it. Please let us know if you have any other questions.

    Regards!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add Custom Validation for a field’ is closed to new replies.