How to know if .keyup() is a character key (jQuery)

前端 未结 7 727
遇见更好的自我
遇见更好的自我 2020-11-29 18:57

How to know if .keyup() is a character key (jQuery)

$(\"input\").keyup(function() {

if (key is a character) { //such as a b A b c 5 3 2 $ # ^ ! ^ * # ...etc         


        
7条回答
  •  难免孤独
    2020-11-29 19:28

    I never liked the key code validation. My approach was to see if the input have text (any character), confirming that the user is entering text and no other characters

    $('#input').on('keyup', function() {
        var words = $(this).val();
        // if input is empty, remove the word count data and return
        if(!words.length) {
            $(this).removeData('wcount');
            return true;
        }
        // if word count data equals the count of the input, return
        if(typeof $(this).data('wcount') !== "undefined" && ($(this).data('wcount') == words.length)){
            return true;
        }
        // update or initialize the word count data
        $(this).data('wcount', words.length);
        console.log('user tiped ' + words);
        // do you stuff...
    });
    
      
      
      
      
      
      
    

提交回复
热议问题