jQuery輕量級(jí)表單模型驗(yàn)證插件
JQuery插件,輕量級(jí)表單模型驗(yàn)證,供大家參考,具體內(nèi)容如下
附上源碼和Demo段
var validataForm = (function(model) {
model.Key = "[data-required='true']";
model.ElementList = new Array();
model.FunctionDictionary = new Dictionary();
model.ToastrCustom = function (msg) {
alert(msg);
}
model.AddElement = function (name) {
model.ElementList.push(name);
};
model.AddFunction = function (name, func) {
model.FunctionDictionary.add(name, func);
};
model.Validata = function (formName) {
for (var i = 0; i < model.ElementList.length; i++) {
var thisObj = model.ElementList[i];
var str = formName + " " + thisObj + model.Key;
var elements = $(str);
for (var j = 0; j < elements.length; j++) {
var element = elements.eq(j);
var value = element.val();
var elementType = element.data().type;
var func = model.FunctionDictionary.find(elementType);
if (func) {
var result = func(value, element);
if (result.status) {
var errorInfo = result.message;
model.ToastrCustom(errorInfo);
return;
}
}
}
}
};
model.ElementList.push("input");
model.ElementList.push("select");
model.FunctionDictionary.add("required", function (value, element) {
var name = element.data().name;
return {
status: (value === ""),
message: (value === "" && name + "不能為空")
};
});
return model;
})(window.validataForm || {});
調(diào)用處
<form id="ValidataForm">
<input data-required="true" data-name="名稱(chēng)" data-type="required" value="">
<input data-required="true" data-name="昵稱(chēng)" data-type="hello">
<button id="Send">提交</button>
</form>
<script src="~/js/plugs/jquery-3.3.1.js"></script>
<script src="~/js/Dictionary.js"></script>
<script src="~/js/ValidataForm.js"></script>
<script type="text/javascript">
$("#Send").click(function () {
validataForm.Validata("#ValidataForm");
});
</script>
Dictionary這個(gè)對(duì)象是抄一個(gè)博主的
代碼附上,內(nèi)置鏈接
/* https://www.cnblogs.com/baiyangyuanzi/p/6689554.html */
/*字典 Dictionary類(lèi)*/
function Dictionary() {
this.add = add;
this.datastore = new Array();
this.find = find;
this.remove = remove;
this.count = count;
this.clear = clear;
}
function add(key, value) {
this.datastore[key] = value;
}
function find(key) {
return this.datastore[key];
}
function remove(key) {
delete this.datastore[key];
}
function count() {
/*var ss = Object.keys(this.datastore).length;
console.log("ssss "+ss);
return Object.keys(this.datastore).length;*/
/**/
var n = 0;
for (var key in Object.keys(this.datastore)) {
++n;
}
return n;
}
function clear() {
for (var key in this.datastore) {
delete this.datastore[key];
}
}
萌新初試前端,有寫(xiě)得不好的地方,望各位前輩,多多指教。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jquery validate.js表單驗(yàn)證的基本用法入門(mén)
- 基于Bootstrap+jQuery.validate實(shí)現(xiàn)Form表單驗(yàn)證
- jquery表單驗(yàn)證使用插件formValidator
- Jquery練習(xí)之表單驗(yàn)證實(shí)現(xiàn)代碼
- jquery+ajax驗(yàn)證不通過(guò)也提交表單問(wèn)題處理
- 基于Jquery實(shí)現(xiàn)表單驗(yàn)證
- jQuery 表單驗(yàn)證插件formValidation實(shí)現(xiàn)個(gè)性化錯(cuò)誤提示
- Jquery插件easyUi表單驗(yàn)證提交(示例代碼)
- jQuery實(shí)現(xiàn)用戶注冊(cè)的表單驗(yàn)證示例
- jquery 實(shí)現(xiàn)表單驗(yàn)證功能代碼(簡(jiǎn)潔)
相關(guān)文章
jqGrid表格應(yīng)用之新增與刪除數(shù)據(jù)附源碼下載
jqGrid可以結(jié)合fancybox等插件完成超酷的彈出層效果,通過(guò)與php后臺(tái)交互,可以輕松完成數(shù)據(jù)的添加與詳情查看,而這個(gè)過(guò)程完全是一個(gè)ajax異步通信過(guò)程,是一個(gè)非常友好的富客戶端應(yīng)用,本文給大家介紹jqGrid表格應(yīng)用之新增與刪除數(shù)據(jù),需要的朋友參考下2015-12-12
Ajax分頁(yè)插件Pagination從前臺(tái)jQuery到后端java總結(jié)
這篇文章主要從前臺(tái)jQuery到后java端總結(jié)了Ajax分頁(yè)插件Pagination的使用方法和技巧,感興趣的小伙伴們可以參考一下2016-07-07
正則驗(yàn)證小數(shù)點(diǎn)后面只能有兩位數(shù)的方法
下面小編就為大家?guī)?lái)一篇正則驗(yàn)證小數(shù)點(diǎn)后面只能有兩位數(shù)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
BootStrap輕松實(shí)現(xiàn)微信頁(yè)面開(kāi)發(fā)代碼分享
本文給大家分享一段代碼關(guān)于bootstrap實(shí)現(xiàn)微信開(kāi)發(fā)頁(yè)面的代碼,非常不錯(cuò)代碼簡(jiǎn)單易懂,感興趣的朋友一起看看吧2016-10-10
jq實(shí)現(xiàn)左側(cè)顯示圖片右側(cè)文字滑動(dòng)切換效果
這篇文章是基于jq技術(shù)實(shí)現(xiàn)左側(cè)顯示圖片右側(cè)文字滑動(dòng)切換效果的相關(guān)資料,有需要的小伙伴可以來(lái)參考下2015-08-08

