Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - on text change while user typing or paste

Tags:

jquery

text

keyup

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>
like image 320
Max Zag Avatar asked Jun 12 '14 17:06

Max Zag


People also ask

How to detect input change jQuery?

Answer: Use the input Event You can bind the input event to an input text box using on() method to detect any change in it. The following example will display the entered value when you type something inside the input field.

How to get text change event in jQuery?

jQuery change() Method The change() method triggers the change event, or attaches a function to run when a change event occurs. Note: For select menus, the change event occurs when an option is selected.

How to detect change in input text JavaScript?

Approach 2: There are few other events that can be used to detect the change in content of textbox. Use any or all of them onchange event, onpropertychange event, onkeyup event, onpaste event and oninput event in the input element and call a function to see the effect.


2 Answers

If you want to capture all input events:

$('#username').on("input", username_check);
like image 136
tymeJV Avatar answered Sep 22 '22 21:09

tymeJV


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

like image 34
Shaunak D Avatar answered Sep 25 '22 21:09

Shaunak D