<script type="text/javascript">
/*$(document).ready(function(){
$("#userid").blur(function(){
$.post("validation.php?input=userid",{ userid:$("#userid").val() } ,function(data){
if(data == "blank"){
$("#useridError").removeClass().addClass('messageError').html('Please do not leave blank!');
return false;
}
});
});
$("#useremail").blur(function(){
$.post("validation.php?input=useremail",{ useremail:$("#useremail").val() } ,function(data){
if(data == "blank"){
$("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
return false;
}
});
});
$("#username").blur(function(){
$.post("validation.php?input=username",{ username:$("#username").val() } ,function(data){
if(data == "blank"){
$("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
return false;
}
});
});
});*/
function validate_userid(){
$.post("validation.php?input=userid",{ userid:$("#userid").val() } ,function(data){
if(data == "blank"){
$("#useridError").removeClass().addClass('messageError').html('Please do not leave blank!');
return false;
}
});
}
function validate_useremail(){
$.post("validation.php?input=useremail",{ useremail:$("#useremail").val() } ,function(data){
if(data == "blank"){
$("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
return false;
}
});
}
function validate_username(){
$.post("validation.php?input=username",{ username:$("#username").val() } ,function(data){
if(data == "blank"){
$("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
return false;
}
});
}
function validate(){
var userid = $("#userid").val();
var useremail = $("#useremail").val();
var username = $("#username").val();
if(userid == ""){
validate_userid();
}else if(useremail == ""){
validate_useremail();
}else if(username == ""){
validate_username();
}
}
/*You can use any one of the validate()--------------the two are working as same-------------------*/
function validate(){
var userid = $("#userid").val();
var useremail = $("#useremail").val();
var username = $("#username").val();
if(userid == ""){
$.post("validation.php?input=userid",{ userid:$("#userid").val() } ,function(data){
if(data == "blank"){
$("#useridError").removeClass().addClass('messageError').html('Please do not leave blank!');
return false;
}
});
}else if(useremail == ""){
$.post("validation.php?input=useremail",{ useremail:$("#useremail").val() } ,function(data){
if(data == "blank"){
$("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
return false;
}
});
}else if(username == ""){
$.post("validation.php?input=username",{ username:$("#username").val() } ,function(data){
if(data == "blank"){
$("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
return false;
}
});
}
}
</script>
<form action="" method="post">
<input type="text" name="userid" id="userid" value="" onblur="return validate_userid();" />
<div id="useridError"></div>
<input type="text" name="useremail" id="useremail" value="" onblur="return validate_useremail();" />
<div id="useremailError"></div>
<input type="text" name="username" id="username" value="" onblur="return validate_username();" />
<div id="usernameError"></div>
<input type="submit" value="SUBMIT" name="submit" id="submit" onclick="return validate();" />
</form>
謝謝,但我使用.blur函數爲用戶提供更好的用戶界面。當焦點移動時,消息將直接顯示給用戶,而不是填寫所有表單,單擊提交按鈕並顯示錯誤消息。 – Bizarre
只需在你的代碼中加入'return false;'這個'$(「#useridError」)。removeClass()。addClass('messageError')。html('請不要留空白! – Kichu
除了userid,我會做一些更多的驗證,這將是$ .post(「#name」)。blur().......我的意思是,讓我說我填寫的電子郵件地址,並得到了問題,當我移動到名稱文本框時,錯誤將顯示在電子郵件地址文本字段旁邊。所以我想阻止表單被提交,如果有任何錯誤信息顯示。 – Bizarre