国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jQuery輕量級表單模型驗證插件

2024-05-06 15:28:46
字體:
供稿:網(wǎng)友

JQuery插件,輕量級表單模型驗證,供大家參考,具體內(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這個對象是抄一個博主的
代碼附上,內(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];  }}            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 闽侯县| 哈尔滨市| 饶阳县| 台湾省| 凤台县| 昌图县| 宽城| 嫩江县| 马公市| 潮州市| 闽侯县| 古交市| 桐城市| 且末县| 永靖县| 门源| 共和县| 綦江县| 鄄城县| 台山市| 凤城市| 温泉县| 肥东县| 黄陵县| 高安市| 报价| 鹤岗市| 武冈市| 永靖县| 莎车县| 五大连池市| 遵义县| 布尔津县| 珲春市| 柯坪县| 达孜县| 平远县| 清新县| 攀枝花市| 浠水县| 辽阳县|