This blog is about how to validate phone number fields in WPForms.
By adding this method, you can make sure users enter only valid phone numbers such as 10-digit formats.
It helps in maintaining correct data for leads or contact forms.
/**
* Add phone number validation in WPForms
*/
function wpforms_custom_phone_validation( $field_id, $field_submit, $form_data ) {
// Optional: Change field ID if needed (1 = Phone field ID)
if ( $field_id == ‘1’ && !empty( $field_submit ) ) {
// Allow only 10 digit numbers
if ( !preg_match( ‘/^[0-9]{10}$/’, $field_submit ) ) {
wpforms()->process->errors[ $form_data[‘id’] ][ $field_id ] = esc_html__( ‘Please enter a valid 10-digit phone number.’, ‘wpforms’ );
}
}
}
add_action( ‘wpforms_process_validate’, ‘wpforms_custom_phone_validation’, 10, 3 );
