這幾天寫項目的時候,後臺引用了一個網上的模版文件,而模版文件中,對與表單的驗證,恰好使用了Validform這個插件,對於這種插件,我不得不說,好用是好用,就是坑太多,一不小心掉進去,就很難受了,百度了半天也不一定有好的結果。
這不,今天下午,我就掉到了坑裏,各種調試,各種百度,浪費了我大量的時間才搞定。其實我覺得吧,表單驗證那麼簡單的問題,沒必要費那麼大力氣,我大概是運氣不好,百度了很多都沒有得到完美的解釋,根據正常的邏輯來,又因爲Validform本身存在的坑無法繞過,所以我寫這篇博客,以示後來者,希望遇到相同或者相似問題的人能看到這篇文章,節約時間。
先說說問題:
這是一個input框,我想先驗證字符長度是否符合條件,再驗證輸入的用戶名是不是在數據庫已存在,若是存在則提示相應文字,若不存在則通過:
代碼:
<input type="text" class="input-text col-xs-12" value="" placeholder="" id="user-name" name="username" datatype="username" nullmsg="用戶名不能爲空" errormsg="用戶名爲2到16位字符">
可以看到我給datatype命名爲username,根據Validform插件的規則 ,下面的js我是這樣寫的:
錯誤的示範1:
datatype:{
username:function(gets,obj,curform,regxp){
var reg = /^\w{2,16}$/;
if(!reg.test(gets)) {
return false;
}
//驗證用戶名是否存在
$.ajax({
url:"doAdminGetAction.php?act=checkAdminExist",
data:{"username":gets},
dataType:'json',
type:'post',
success:function(data){
if(data.status == 0){
console.log(data.info)
return "此用戶已存在";
}else{
console.log(data.info)
return true;
}
},
error:function(data){
console.log(data);
}
});
}
},
錯誤的示範2:
datatype:{
username:function(gets,obj,curform,regxp){
var reg = /^\w{2,16}$/;
if(!reg.test(gets)) {
return false;
}
//驗證用戶名是否存在
var result;
$.ajax({
url:"doAdminGetAction.php?act=checkAdminExist",
data:{"username":gets},
dataType:'json',
type:'post',
success:function(data){
if(data.status == 0){
console.log(data.info)
result = data.info;
}else{
console.log(data.info)
result = true;
}
},
error:function(data){
console.log(data);
}
});
console.log(data);
return result;
}
},
正確的示範:
datatype:{
username:function(gets,obj,curform,regxp){
var reg = /^\w{2,16}$/;
if(!reg.test(gets)) {
return false;
}
//驗證用戶名是否存在
var result;
$.ajax({
url:"doAdminGetAction.php?act=checkAdminExist",
data:{"username":gets},
dataType:'json',
type:'post',
async:false,
success:function(data){
if(data.status == 0){
console.log(data.info)
result = data.info;
}else{
console.log(data.info)
result = true;
}
},
error:function(data){
console.log(data);
}
});
return result;
}
},
運行一下,ok!//表單驗證提交
$("#form-admin-add").Validform({
tiptype:2,
tipSweep:false,
submitForm:true,
ignoreHidden:true,
postonce:true,
ajaxPost:true,
datatype:{
username:function(gets,obj,curform,regxp){
var reg = /^\w{2,16}$/;
if(!reg.test(gets)) {
return false;
}
//驗證用戶名是否存在
var result;
$.ajax({
url:"doAdminGetAction.php?act=checkAdminExist",
data:{"username":gets},
dataType:'json',
type:'post',
async:false,
success:function(data){
if(data.status == 0){
console.log(data.info)
result = data.info;
}else{
console.log(data.info)
result = true;
}
},
error:function(data){
console.log(data);
}
});
return result;
}
},
callback:function(data){
console.log(data);
//form[0].submit();
if(data.status==1){
layer.msg(data.info, {icon: data.status,time: 1000}, function(){
location.reload();//刷新頁面
});
}else if(data.status==3){
layer.msg(data.info, {icon: data.status,time: 1000});
}else{
layer.msg(data.info, {icon: data.status,time: 3000});
}
var index =parent.$("#iframe").attr("src");
parent.layer.close(index);
//
}
});
後端代碼:if($act == 'checkAdminExist'){
$username = $_POST['username'];
$sql = "select id from shiguang_admin where username='{$username}'";
$rows = fetchOne($link,$sql);//獲取是否有這個用戶
if($rows){
echo ajaxReturn(0, "此管理員名已存在");
}else{
echo ajaxReturn(1, "此管理員名可用");
}
}
/**
* 返回ajax需求的數據
* @param int $status
* @param char $msg
* @param string/array $data
* @param string $type
* @return string 所需格式字符串
*/
function ajaxReturn($status,$msg,$data=null,$type="json"){
$res = [
'info'=>$msg,
'status'=>$status,
'data'=>$data
];
if($type == "json"){
header('Content-type: application/json');
return json_encode($res);
}else if($type == "xml"){
header('Content-Type:text/xml; charset=utf-8');
return arrayToXml($res);
}else{
exit("無此數據格式");
}
}