亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用bootstrap validator的remote驗(yàn)證代碼經(jīng)驗(yàn)分享(推薦)

 更新時(shí)間:2016年09月21日 09:19:52   作者:Franson  
這篇文章主要介紹了使用bootstrap validator的remote驗(yàn)證器驗(yàn)證經(jīng)驗(yàn)分享(推薦)的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下

這里需要說一下,bootstrapvalidator的幫助文檔寫的比較簡(jiǎn)單,對(duì)于remote驗(yàn)證器的說明更是如此,在經(jīng)歷多方測(cè)試之后才明白如何使用這個(gè)驗(yàn)證器。

一個(gè)典型的ajax驗(yàn)證代碼如下:

服務(wù)端驗(yàn)證代碼(使用spring mvc)如下:

/*
* 返回String類型的結(jié)果
* 檢查用戶名的合法性,如果用戶已經(jīng)存在,返回false,否則返回true(返回json數(shù)據(jù),格式為{"valid",true})
*/
@RequestMapping(value = "/checkNameExistsMethod1", produces = "application/json;charset=UTF-8")
public @ResponseBody
String checkNameValidMethod1(@RequestParam String name) {
boolean result = true;
List<Employee> lstEmployees = employeeService.getAllEmployees();
for (Employee employee : lstEmployees) {
if (employee.getName().equals(name)) {
result = false;
break;
}
}
Map<String, Boolean> map = new HashMap<>();
map.put("valid", result);
ObjectMapper mapper = new ObjectMapper();
String resultString = "";
try {
resultString = mapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return resultString;
}

這里需要說明的是bootstrap的remote驗(yàn)證器需要的返回結(jié)果一定是json格式的數(shù)據(jù) :

{"valid":false} //表示不合法,驗(yàn)證不通過
{"valid":true} //表示合法,驗(yàn)證通過

如果返回任何其他的值,頁面驗(yàn)證將獲取不到驗(yàn)證結(jié)果導(dǎo)致無法驗(yàn)證。

附一段完整的遠(yuǎn)程remote驗(yàn)證的代碼加說明:

$(function(){/* 文檔加載,執(zhí)行一個(gè)函數(shù)*/
$('#defaultForm').bootstrapValidator({
message: 'This value is not valid',
feedbackIcons: {/*input狀態(tài)樣式圖片*/
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {/*驗(yàn)證:規(guī)則*/
username: {//驗(yàn)證input項(xiàng):驗(yàn)證規(guī)則
message: 'The username is not valid',
validators: {
notEmpty: {//非空驗(yàn)證:提示消息
message: '用戶名不能為空'
},
stringLength: {
min: 6,
max: 30,
message: '用戶名長度必須在6到30之間'
},
threshold : 6 , //有6字符以上才發(fā)送ajax請(qǐng)求,(input中輸入一個(gè)字符,插件會(huì)向服務(wù)器發(fā)送一次,設(shè)置限制,6字符以上才開始)
remote: {//ajax驗(yàn)證。server result:{"valid",true or false} 向服務(wù)發(fā)送當(dāng)前input name值,獲得一個(gè)json數(shù)據(jù)。例表示正確:{"valid",true} 
url: 'exist2.do',//驗(yàn)證地址
message: '用戶已存在',//提示消息
delay : 2000,//每輸入一個(gè)字符,就發(fā)ajax請(qǐng)求,服務(wù)器壓力還是太大,設(shè)置2秒發(fā)送一次ajax(默認(rèn)輸入一個(gè)字符,提交一次,服務(wù)器壓力太大)
type: 'POST'//請(qǐng)求方式
/**自定義提交數(shù)據(jù),默認(rèn)值提交當(dāng)前input value
* data: function(validator) {
return {
password: $('[name="passwordNameAttributeInYourForm"]').val(),
whatever: $('[name="whateverNameAttributeInYourForm"]').val()
};
}
*/
},
regexp: {
regexp: /^[a-zA-Z0-9_\.]+$/,
message: '用戶名由數(shù)字字母下劃線和.組成'
}
}
},
password: {
message:'密碼無效',
validators: {
notEmpty: {
message: '密碼不能為空'
},
stringLength: {
min: 6,
max: 30,
message: '用戶名長度必須在6到30之間'
},
identical: {//相同
field: 'password', //需要進(jìn)行比較的input name值
message: '兩次密碼不一致'
},
different: {//不能和用戶名相同
field: 'username',//需要進(jìn)行比較的input name值
message: '不能和用戶名相同'
},
regexp: {
regexp: /^[a-zA-Z0-9_\.]+$/,
message: 'The username can only consist of alphabetical, number, dot and underscore'
}
}
},
repassword: {
message: '密碼無效',
validators: {
notEmpty: {
message: '用戶名不能為空'
},
stringLength: {
min: 6,
max: 30,
message: '用戶名長度必須在6到30之間'
},
identical: {//相同
field: 'password',
message: '兩次密碼不一致'
},
different: {//不能和用戶名相同
field: 'username',
message: '不能和用戶名相同'
},
regexp: {//匹配規(guī)則
regexp: /^[a-zA-Z0-9_\.]+$/,
message: 'The username can only consist of alphabetical, number, dot and underscore'
}
}
},
email: {
validators: {
notEmpty: {
message: '郵件不能為空'
},
emailAddress: {
message: '請(qǐng)輸入正確的郵件地址如:123@qq.com'
}
}
},
phone: {
message: 'The phone is not valid',
validators: {
notEmpty: {
message: '手機(jī)號(hào)碼不能為空'
},
stringLength: {
min: 11,
max: 11,
message: '請(qǐng)輸入11位手機(jī)號(hào)碼'
},
regexp: {
regexp: /^1[3|5|8]{1}[0-9]{9}$/,
message: '請(qǐng)輸入正確的手機(jī)號(hào)碼'
}
}
},
invite: {
message: '邀請(qǐng)碼',
validators: {
notEmpty: {
message: '邀請(qǐng)碼不能為空'
},
stringLength: {
min: 8,
max: 8,
message: '請(qǐng)輸入正確長度的邀請(qǐng)碼'
},
regexp: {
regexp: /^[\w]{8}$/,
message: '請(qǐng)輸入正確的邀請(qǐng)碼(包含數(shù)字字母)'
}
}
},
}
})
.on('success.form.bv', function(e) {//點(diǎn)擊提交之后
// Prevent form submission
e.preventDefault();
// Get the form instance
var $form = $(e.target);
// Get the BootstrapValidator instance
var bv = $form.data('bootstrapValidator');
// Use Ajax to submit form data 提交至form標(biāo)簽中的action,result自定義
$.post($form.attr('action'), $form.serialize(), function(result) {
//do something...
});
});
});

以上所述是小編給大家介紹的使用bootstrap validator的remote驗(yàn)證經(jīng)驗(yàn)分享(推薦),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論