jQuery - on text change while user typing or paste
Asked Answered
W

2

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>
Westbound answered 12/6, 2014 at 17:32 Comment(1)
possible duplicate of How do I capture the input value on a paste event?Chronology
Z
40

If you want to capture all input events:

$('#username').on("input", username_check);
Zippora answered 12/6, 2014 at 17:35 Comment(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.Patricapatrice
IE9 though doesn't fully support that event (caniuse.com/#feat=input-event).Monastic
J
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

Jakie answered 12/6, 2014 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.