Validation custom field
-
I need help posting a validation code into my registration form. I was trying to follow the coding from this post from two years ago https://ww.wp.xz.cn/support/topic/add-custom-validation-for-a-field/.
I have this code written, can anyone help me with getting the code right? I need to validate the field name “enrollmentnumber” so a user cant register using the same #.
Here is what i have:
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( 'enrollmentnumber' === $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_enrollmentnumber' 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( 'enrollmentnumber' === $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' );
});
}
}
}
}
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
The topic ‘Validation custom field’ is closed to new replies.