JQuery - when changing text during user input or insertion

I am new to jQuery and I tried to do something, but I failed, so here is my problem when the user enters it, but when the user inserts something, it does not work!

$(document).ready(function(){

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

username_check function:

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

field:

<input type="text" id="username" name="username" required>
+4
source share
2 answers

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 script

+11
source

If you want to capture all events input:

$('#username').on("input", username_check);
+8
source

Source: https://habr.com/ru/post/1544370/


All Articles