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="名稱" data-type="required" value="">
<input data-required="true" data-name="昵稱" 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類*/
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];
}
}
萌新初試前端,有寫得不好的地方,望各位前輩,多多指教。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jquery validate.js表單驗(yà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ò)也提交表單問題處理
- 基于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)文章
基于jquery實(shí)現(xiàn)最簡(jiǎn)單的選項(xiàng)卡切換效果
這篇文章主要介紹了基于jquery實(shí)現(xiàn)最簡(jiǎn)單的選項(xiàng)卡切換效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2016-05-05
jquery入門—選擇器實(shí)現(xiàn)隔行變色實(shí)例代碼
JQuery入門—選擇器實(shí)現(xiàn)隔行變色如何實(shí)現(xiàn)呢?JQuery選擇器繼承了CSS、path語(yǔ)音的部分語(yǔ)法,允許通過(guò)標(biāo)簽名、屬性名、內(nèi)容對(duì)DOM元素進(jìn)行快速、準(zhǔn)確的選擇,接下來(lái)詳細(xì)介紹,需要的朋友可以參考下2013-01-01
jQuery實(shí)現(xiàn)新消息閃爍標(biāo)題提示的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)新消息閃爍標(biāo)題提示的方法,實(shí)例分析了jQuery操作樣式的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
基于jQuery的煙花效果(運(yùn)動(dòng)相關(guān))點(diǎn)擊屏幕出煙花
基于jQuery的煙花效果(運(yùn)動(dòng)相關(guān))點(diǎn)擊屏幕出煙花 ,經(jīng)測(cè)試不能點(diǎn)擊過(guò)多,容易假死2012-06-06

