Home / Admin / Disable the space key with the special CSS class
Duplicate Snippet

Embed Snippet on Your Site

Disable the space key with the special CSS class

This code snippet disables the space key. When a user presses the space key in these fields, the keypress is prevented, making it impossible to enter spaces.

<10
Code Preview
php
<?php
/**
 * Disable the space key with the special CSS class
 * Apply the class "wpf-disable-space" to the field to enable.
 *
 * @link https://wpforms.com/developers/how-to-disable-the-space-key-inside-a-form-field/
 *
 */
   
function wpf_dev_disalbe_space() {
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function ($) {
             
            jQuery( '.wpf-disable-space input' ).keydown(function(e) {
                 
                if (event.key === ' ') {
                    return false;
                }
                 
            });
             
    });
    </script>
    <?php
}
add_action( 'wpforms_wp_footer_end', 'wpf_dev_disalbe_space', 10 );

Comments

Add a Comment