How to Restrict Numbers in a Single Line Text Form Field


Would you like to restrict your visitors from using a number in a particular text field? With a small PHP snippet, you can easily restrict a Single Line Text form field to accept letters only. In this tutorial, we’ll show you the necessary code snippet to restrict numbers in these fields.


Once you’ve set up your form and fields, the next step is to add this code snippet to your site.

In our example, we’re going to prevent numbers inside our Single Line Text form field $fields[11] for the form ID 1049.

 * Disallow numbers in a single-line text field
 * @link
function wpf_dev_process( $fields, $entry, $form_data ) {
    // Optional, you can limit to specific forms. Below, we restrict output to
    // form ID #1049.

    if ( absint( $form_data['id'] ) !== 1049 ) {
        return $fields;
    // get value of the specific field ID and set it to a variable

    $mystring = $fields[11]['value'];

    if (!preg_match ('/^([a-zA-Z]+)$/', $mystring))  
            // Add to global errors. This will stop form entry from being saved to the database.
            // Uncomment the line below if you need to display the error above the form.
            // wpforms()->process->errors[ $form_data['id'] ]['header'] = esc_html__( 'Some error occurred.', 'plugin-domain' );    
            // Check the field ID 4 and show error message at the top of form and under the specific field
               wpforms()->process->errors[ $form_data['id'] ] [ '11' ] = esc_html__( 'This field cannot accept numbers.', 'plugin-domain' );
            // Add additional logic (what to do if error is not displayed)

add_action( 'wpforms_process', 'wpf_dev_process', 10, 3 );

And that’s it! When your visitors try to input a number into that field, they will see an error message on the form when the form submits.

now you can restrict numbers in a single line text form field

Would you also like to prevent special characters from being entered into a Single Line Text form field? Check out the tutorial on How to Restrict Special Characters From a Form Field.

Action Reference: wpforms_process


Q: What if I want to prevent the numbers from the field prior to the form submitting?

A: In order to prevent any numbers from being entered before the form is submitted, please use this snippet.

function wpf_dev_restrict_numbers( ) {
  <script type="text/javascript">
    (function($) {
      var No_Numbers_Custom_Validation = {
        init: function() {
          $(document).on('wpformsReady', No_Numbers_Custom_Validation.customRules);
        customRules: function() {
          if (typeof $.fn.validate !== 'undefined') { 
            $.validator.addMethod("check_for_number", function(value, element, param) {
              var $ele = $(element);
              return this.optional(element) || ! /\d/.test(value);
            }, $.validator.format("This field cannot accept numbers."));
          if ( $('.no-numbers input').length ) {
            $('.no-numbers input').rules( 'add', {
              check_for_number: true
add_action( 'wpforms_wp_footer_end', 'wpf_dev_restrict_numbers', 30 );

And then add the CSS class nameno-numbers to the Single Line Text form field on the Advanced tab of the form field settings.