Allow text box only for letters using jQuery?

后端 未结 11 1558
情书的邮戳
情书的邮戳 2020-11-29 04:42

I want to make a text box allow only letters (a-z) using jQuery. Any examples?

相关标签:
11条回答
  • 2020-11-29 05:14

    Solution described by @dev-null-dweller is working absolutely.

    However, As of jQuery 3.0, .bind() method has been deprecated. It was superseded by the .on() method for attaching event handlers to a document since jQuery 1.7, so its use was already discouraged.

    Check deprecated methods list for jQuery 3.0 here: http://api.jquery.com/category/deprecated/deprecated-3.0/

    So the solution is to use .on() method instead .bind().

    If you need to bind existing elements then the code will be :

    $('.alphaonly').on('keyup blur', function(){
        var node = $(this);
        node.val( node.val().replace(/[^a-z]/g,'') ); 
    }); 
    

    If you need to bind to dynamic elements the code will be :

    $(document).on('keyup blur', '.alphaonly', function(){
        var node = $(this);
        node.val(node.val().replace(/[^a-z]/g,'') );
    });
    

    You need to bind the event to document or some other element that already exist from the document load.

    Hope this is helpful for new version of jQuery.

    0 讨论(0)
  • 2020-11-29 05:16

    Thanks to the first answer.. made this..

    <input name="lorem" class="alpha-only">
    
    <script type="text/javascript">
       $(function() 
       {
            $('.alpha-only').bind('keyup input',function()
            {       
                if (this.value.match(/[^a-zA-Z áéíóúÁÉÍÓÚüÜ]/g)) 
                {
                    this.value = this.value.replace(/[^a-zA-Z áéíóúÁÉÍÓÚüÜ]/g, '');
                }
            });
        });
    </script>
    

    This has some improvements like letters with accents, and changing "blur" for "input" corrects the Non-letters displayed momentarily, also when you select text with the mouse and dragging is corrected..

    0 讨论(0)
  • 2020-11-29 05:17

    Supports backspace:

    new RegExp("^[a-zA-Z \b]*$");
    

    This option will not check mobile. So you can use a jQuery Mask Plugin and use following code:

    jQuery('.alpha-field, input[name=fname]').mask('Z',{translation:  {'Z': {pattern: /[a-zA-Z ]/, recursive: true}}});
    
    0 讨论(0)
  • 2020-11-29 05:29

    JQuery function to allow only small and Capital Letters:

    Text Field:

    <input id="a" type="text" />
    

    JQuery Function:

    $('#a').keydown(function (e) {
        if (e.ctrlKey || e.altKey) {
               e.preventDefault();
        } else {
               var key = e.keyCode;
               if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
                    e.preventDefault();
               }
        }
    });
    
    0 讨论(0)
  • 2020-11-29 05:33
    <input name="lorem" onkeyup="this.value=this.value.replace(/[^a-z]/g,'');">
    

    And can be the same to onblur for evil user who like to paste instead of typing ;)

    [+] Pretty jQuery code:

    <input name="lorem" class="alphaonly">
    <script type="text/javascript">
    $('.alphaonly').bind('keyup blur',function(){ 
        var node = $(this);
        node.val(node.val().replace(/[^a-z]/g,'') ); }
    );
    </script>
    
    0 讨论(0)
提交回复
热议问题