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>
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.
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.
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.
If you want to capture all input
events:
$('#username').on("input", username_check);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With