25

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>
1

2 Answers 2

41

If you want to capture all input events:

$('#username').on("input", username_check);
Sign up to request clarification or add additional context in comments.

2 Comments

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.
IE9 though doesn't fully support that event (caniuse.com/#feat=input-event).
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.