php - javascript function always returns false -
this question has answer here:
- how return response asynchronous call? 21 answers
i trying validate html form using jquery,ajax , php. i've written function in jquery checks availability of username using ajax post request.however, problem is returning false. i'm new jquery , javascript. i've tried quite bit i'm unable figure out problem. please help.
function isvalid_username() { var username=$('#username').val(); var option=false; if(username == "") { $('#username').removeclass('valid').addclass('invalid'); $('#username_feedback').addclass('error').html("please choose username!!"); option=false; } else if(username.length < 4) { $('#username').removeclass('valid').addclass('invalid'); $('#username_feedback').addclass('error').html("your username must atleast 4 characters."); option=false; } else { $.post('php/check_username_avail.php',{ username : username },function(data){ if(data == "false") { $('#username').removeclass('valid').addclass('invalid'); $('#username_feedback').addclass('error').html("sorry! username taken. try different one."); option= false; } else if(data == "true") { $('#username').removeclass('invalid').addclass('valid'); $('#username_feedback').html(""); alert('now should return true'); option=true; } else { $('#username').removeclass('valid').addclass('invalid'); $('#username_feedback').addclass('error').html(data); option=false; } }).error(function(){ alert("an error occurred. unable validate username"); }); } return option; }
edit: i've read other post. still don't find way validate form. want use submit handler function code:
$('#signup_form').submit(function(event) { if(isvalid_first_name() && isvalid_username() && isvalid_email() && isvalid_password()) { alert('submitting form'); } else { event.preventdefault(); $('#submit_feedback').html("please correct above problems first"); } });
if can't done way, other technique should use validate form on clientside?
well because not waiting ajax complete , returning option right away false. try below code.
function isvalid_username(){ var username=$('#username').val(); var option=false; if(username == "") { $('#username').removeclass('valid').addclass('invalid'); $('#username_feedback').addclass('error').html("please choose username!!"); return option; } else if(username.length < 4) { $('#username').removeclass('valid').addclass('invalid'); $('#username_feedback').addclass('error').html("your username must atleast 4 characters."); return option; } else { $.post('php/check_username_avail.php',{ username : username },function(data){ if(data == "false") { $('#username').removeclass('valid').addclass('invalid'); $('#username_feedback').addclass('error').html("sorry! username taken. try different one."); return option; } else if(data == "true") { $('#username').removeclass('invalid').addclass('valid'); $('#username_feedback').html(""); alert('now should return true'); option=true; return option; } else { $('#username').removeclass('valid').addclass('invalid'); $('#username_feedback').addclass('error').html(data); return option; } }).error(function(){ alert("an error occurred. unable validate username"); }); } }
Comments
Post a Comment