24

I'm new to jQuery and I've tried to make something but I failed so here is my problem when the user type it works but when the user paste something didn't work !!

$(document).ready(function(){

        $('#username').keyup(username_check);
});

the username_check function :

function username_check(){  
var username = $('#username').val();
if(username == "" || username.length < 4){
alert("error");
}

the field :

<input type="text" id="username" name="username" required>
Max Zag
  • 249
  • 1
  • 2
  • 5
  • possible duplicate of [How do I capture the input value on a paste event?](http://stackoverflow.com/questions/6902455/how-do-i-capture-the-input-value-on-a-paste-event) – GillesC Jun 12 '14 at 17:38

2 Answers2

39

If you want to capture all input events:

$('#username').on("input", username_check);
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • 2
    This worked perfectly for me. I want to turn a button on when someone types anything into a box. removed on change listener and added this. – chrisallick Jan 06 '17 at 15:43
  • 1
    IE9 though doesn't fully support that event (https://caniuse.com/#feat=input-event). – Kenston Choi Dec 28 '18 at 05:10
25

Use .on() or .bind() to bind multiple events,

$(document).ready(function(){
   $('#username').on('keyup paste',username_check);
});


function username_check(){ 
    setTimeout( function() {
        var username = $('#username').val();
    },100);
    if(username == "" || username.length < 4){
      alert("error");
    }
}

Working Fiddle

Shaunak D
  • 20,588
  • 10
  • 46
  • 79