Jquery username check
I'm using this Jquery function for available username check.
How can I fire this Jquery function only if the username field is greater of 5 characters?
Jquery looks like:
$(document).ready(function() {
$('#usernameLoading').hide();
$('#username').blur(function(){
$('#usernameLoading').show();
$.post("usercheck.php", {
un: $('#username').val()
}, function(response){
$('#usernameResult').fadeOut();
setTimeout("finishAjax('usernameResult', '"+escape(response)+"')", 400);
});
return false;
});
});
function finishAjax(id, response) {
$('#usernameLoading').hide();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
} //finishAjax
Can I use something like this and how:
开发者_JAVA百科var usr = $("#username").val();
if(usr.length >= 5)
{
}
$(document).ready(function() {
$('#usernameLoading').hide();
$('#username').blur(function(){
if ($("#username").val().length < 5) {
return;
}
$('#usernameLoading').show();
$.post("usercheck.php", {
un: $('#username').val()
}, function(response){
$('#usernameResult').fadeOut();
setTimeout("finishAjax('usernameResult', '"+escape(response)+"')", 400);
});
return false;
});
});
function finishAjax(id, response) {
$('#usernameLoading').hide();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
} //finishAjax
Pretty close, it should work like that:
if($("#username")[0].value.length >= 5) {
// do something
}
Should be able to. Just put it in under $('#username').blur(function(){
and wrap the current block in the if
statement
$('#username').keypress(function(){
var name = $(this).val();
if (name.length > 5) doAjaxCheck(name);
});
I split the doAjaxCheck into a separate function because you may want to check it again because of clipboard operations etc.
精彩评论