Home / Admin / How to Restrict Numbers in a Single Line Text Form Field
Duplicate Snippet

Embed Snippet on Your Site

How to Restrict Numbers in a Single Line Text Form Field

<10
Code Preview
php
<?php
/**
 * Disallow numbers in a single-line text field
 *
 * @link https://wpforms.com/developers/how-to-restrict-numbers-in-a-single-line-text-form-field/
 */
 
function wpf_dev_disallow_numbers_text_field( $fields, $entry, $form_data ) {
         
    // Optional, you can limit to specific forms. Below, we restrict output to
    // form ID #1000.
    if ( absint( $form_data[ 'id' ] ) !== 1000 ) {
        return $fields;
    }
         
    // get value of the specific field ID and set it to a variable
    // field ID #25
    $mystring = $fields[25][ 'value' ];
  
    if (!preg_match ('/^([a-zA-Z]+)$/', $mystring))  
      {
        // Check the field ID 25 and show error message at the top of form and under the specific field
        wpforms()->process->errors[ $form_data[ 'id' ] ] [ '25' ] = esc_html__( 'The username can only contain letters.', 'plugin-domain' );
      }
     
}
  
add_action( 'wpforms_process', 'wpf_dev_disallow_numbers_text_field', 10, 3 );

Comments

Add a Comment