之前讲了一部分揭秘系列的东西,由于年初的时候在改项目,也没有写下去。现在开始闲下来了,会继续写没写完的东西,各种原生js实现Jquery的功能。

转入正题,说一下今天要讲的东西。

相信很多tx在项目里面写过这样的js代码:

$("#..").click(function(){

var   val=$("#..").val();

if(!val)

{

alert('.......');

return    false;

}

if(!/...../.test(val))

{

alert('.......');

return    false;

}

............各种验证

});

有没有觉得每次写的都是重复的东西,并且看起来不是很优雅,验证完全是可以写通用的。

有的tx会说,我用的是Mvc的验证框架 ,只需要在后台代码的类的成员属性上面标记各种attribute,就能进行验证。这样确实是方便,但是不是很好控制。

我觉得以下代码的写法看起来更直观,优雅:

 var objtest = {
rules: {
col1: {
required: true,
max: 99,
min: 10,
reg: /^a/gi,
remote: 'test.ashx'
},
col2: {
required: true
}
},
msg: {
col1: {
required: 'col1必填',
max: '最大不能超过99',
min: '最小不能小于10',
reg: '必须以a开头'
},
col2: {
required: 'col2必填' }
} } $("#form").Validate(objtest);

没错,这就是 validate框架里面的验证写法。

讲到这个话题,你需要做一下功课,了解一下jquery.validate.js。我现在是要讲一下里面的实现原理:

代码里面我只写了四个基本的验证:是否必填,最大值,最小值,正则匹配。

这里我写一个简单的id选择器,同样是用$符号,看过我的博客的同学应该看到过。

  var $ = function () {
var arr = Array.prototype.slice.call(arguments);
return new $.prototype.Init(arr.length > ? arr[] : null);
}

下面继续扩展一些原型方法:

$.prototype = {
                Init: function (id) {
                    this.form = document.getElementById(id);
                    this.childs = this.form.childNodes;
                },
                Validate:function(){

这段代码单独拿出来。。。

}

}

Validate方法里面有个处理错误信息的div:

                   var that = this;
///移除错误提醒的div
var removeDiv = function () {
var getErrdiv = document.getElementById('errmsg');
if (getErrdiv) {
that.form.removeChild(getErrdiv);
}
}
///创建一个错误提醒的div
var creatDiv = function (msg, objset) {
var div = document.createElement('div');
div.style.backgroundColor = 'red';
div.id = 'errmsg';
div.innerHTML = msg;
that.form.appendChild(div);
}

然后就是   form   的onsubmit  事件:

                    this.form.onsubmit = function () {
var msg = "";
var checked = true;
removeDiv();
if (obj.rules) {
for (var i in obj.rules) {
var col = obj.rules[i];
///必填验证
if (col.required) {
for (var m = 0; m < that.childs.length; m++) {
if (that.childs[m].id == i && !that.childs[m].value) {
msg = obj.msg[i].required;
that.childs[m].focus();
creatDiv(msg);
return false;
}
} }
///最大值验证
if (col.max) {
for (var m = 0; m < that.childs.length; m++) {
if (that.childs[m].id == i && that.childs[m].value > col.max) {
msg = obj.msg[i].max;
creatDiv(msg);
that.childs[m].focus();
return false;
}
} }
///最小值验证
if (col.min) {
for (var m = 0; m < that.childs.length; m++) {
if (that.childs[m].id == i && that.childs[m].value < col.min) {
msg = obj.msg[i].min;
creatDiv(msg);
that.childs[m].focus();
return false;
}
} }
////正则匹配
if (col.reg) {
for (var m = 0; m < that.childs.length; m++) { if (that.childs[m].id == i && (!col.reg.test(that.childs[m].value))) { msg = obj.msg[i].reg;
creatDiv(msg); that.childs[m].focus(); return false;
}
}
} } }
return checked;
}

这里面其实是用对象的属性对应控件的id ,然后分别做验证,只是把逻辑放在一块集中做了处理。

完整代码   :

   var $ = function () {
var arr = Array.prototype.slice.call(arguments);
return new $.prototype.Init(arr.length > 0 ? arr[0] : null);
}
$.prototype = {
Init: function (id) {
this.form = document.getElementById(id);
this.childs = this.form.childNodes;
},
Validate: function (obj) {
var that = this;
///移除错误提醒的div
var removeDiv = function () {
var getErrdiv = document.getElementById('errmsg');
if (getErrdiv) {
that.form.removeChild(getErrdiv);
}
}
///创建一个错误提醒的div
var creatDiv = function (msg, objset) {
var div = document.createElement('div');
div.style.backgroundColor = 'red';
div.id = 'errmsg';
div.innerHTML = msg;
that.form.appendChild(div);
} this.form.onsubmit = function () {
var msg = "";
var checked = true;
removeDiv();
if (obj.rules) {
for (var i in obj.rules) {
var col = obj.rules[i];
///必填验证
if (col.required) {
for (var m = 0; m < that.childs.length; m++) {
if (that.childs[m].id == i && !that.childs[m].value) {
msg = obj.msg[i].required;
that.childs[m].focus();
creatDiv(msg);
return false;
}
} }
///最大值验证
if (col.max) {
for (var m = 0; m < that.childs.length; m++) {
if (that.childs[m].id == i && that.childs[m].value > col.max) {
msg = obj.msg[i].max;
creatDiv(msg);
that.childs[m].focus();
return false;
}
} }
///最小值验证
if (col.min) {
for (var m = 0; m < that.childs.length; m++) {
if (that.childs[m].id == i && that.childs[m].value < col.min) {
msg = obj.msg[i].min;
creatDiv(msg);
that.childs[m].focus();
return false;
}
} }
////正则匹配
if (col.reg) {
for (var m = 0; m < that.childs.length; m++) { if (that.childs[m].id == i && (!col.reg.test(that.childs[m].value))) { msg = obj.msg[i].reg;
creatDiv(msg); that.childs[m].focus(); return false;
}
}
} } }
return checked;
}
}
} $.prototype.Init.prototype = $.prototype;

代码看着没啥难度,比较简单 。

调用方法如下  :

  <form id="test">
<input id="col1" type="text" />
<input id="col2" type="text" />
<input id="Submit1" type="submit" value="submit" />
</form>
window.onload=function(){

  var objtest = {
rules: {
col1: {
required: true,
max: 99,
min: 10,
reg: /^a/gi,
remote: 'test.ashx'
},
col2: {
required: true }
},
msg: {
col1: {
required: 'col1必填',
max: '最大不能超过99',
min: '最小不能小于10',
reg: '必须以a开头'
},
col2: {
required: 'col2必填' }
} } $("test").Validate(objtest); }

代码可以继续优化,添加各种验证方法。这里只是抛砖引玉。有啥意见或者疑问可以联系:QQ546558309,或者留言。

下一节会讲ajax的原生js实现。

Jquery揭秘系列:Validation实现的更多相关文章

  1. Jquery揭秘系列:实现$.fn.extend 和$.extend函数

    前面我们扩展了bind方法和ready函数,这次我要讲一下$.fn.extend 和$.extend函数. 其他的不多说,直接切入主题吧! 先来看看这两个函数的区别: $.fn.extend是为查询的 ...

  2. Jquery揭秘系列:谈谈bind,one,live,delegate事件及实现

    在Jquery里面,我们用的最多的就是事件绑定了,事件绑定有多个函数.例如:bind,one,live,delegate等等. 我们先看看他们的定义,直接进入主题: bind( )方法用于将一个处理程 ...

  3. Jquery揭秘系列:实现 ready和bind事件

    讲这一节之前,先回顾之前的一篇<小谈Jquery>里面的代码: (function (win) { var _$ = function (selector, context) { retu ...

  4. Jquery揭秘系列:谈谈bind,one,live,delegate,on事件及实现

    在Jquery里面,我们用的最多的就是事件绑定了,事件绑定有多个函数.例如:bind,one,live,delegate,on等等. on() jQuery事件绑定.on()简要概述及应用 看源码发现 ...

  5. Jquery揭秘系列:ajax原生js实现

    讲到ajax这个东西,我们要知道两个对象XMLHTTPRequest和ActiveXObject ,提供了对 HTTP 协议的完全的访问,包括做出 POST 和 HEAD 请求以及普通的 GET 请求 ...

  6. 深入学习jQuery选择器系列第一篇——基础选择器和层级选择器

    × 目录 [1]id选择器 [2]元素选择器 [3]类选择器[4]通配选择器[5]群组选择器[6]后代选择器[7]兄弟选择器 前面的话 选择器是jQuery的根基,在jQuery中,对事件处理.遍历D ...

  7. 深入学习jQuery选择器系列第八篇——过滤选择器之伪子元素选择器

    × 目录 [1]通用形式 [2]反向形式 [3]首尾元素 [4]唯一元素 前面的话 本文是子元素选择器的续篇,主要介绍关于nth-of-type()选择器的内容.该部分内容并非没有出现在<锋利的 ...

  8. 【JQuery NoviceToNinja系列】目录

    [JQuery NoviceToNinja系列]目录 [JQuery NoviceToNinja系列]01 开篇 Html页面设计和布局

  9. 【原创】书本翻页效果booklet jquery插件系列之简介

    booklet jquery插件系列之简介 本文由五月雨恋提供,转载请注明出处. 一.安装 1.添加CSS和Javascript 添加booklet CSS文件到你的页面. <link rel= ...

随机推荐

  1. 微信小程序开发总结

    一.设计 无需开发者开发的 1.小程序加载动画: 2.页面下拉刷新加载样式: 3.微信控件(拥有完整的操作反馈):如弹出框.通知.模态框...   建议用微信自己的 1.加载.反馈样式(全局.局部) ...

  2. JavaScript基本语法(二)

    上篇博文写到JavaScript的数据类型.JavaScript包括了字符串(String).数字(Number).布尔(Boolean).数组(Array).对象(Object).空(Null).未 ...

  3. mysql时间加减函数

    先来看一个例子: select now(),now()+0; 可以看到now()函数可以返回一个时间也可以返回一个数字,就看大家如何使用.如果相对当前时间进行增加或减少,并不能直接加上或减去一个数字而 ...

  4. 关于SharePoint 2013的工作流(一)

    从去年开始,一直和SharePoint 2013工作流打交道.自己瞎摸索,以实现功能为目的.直到如今也不知道走的路是否正确. 一开始用WF4发现整个都不一样了,用的xaml无法写后端代码.Google ...

  5. [Animatable Properties](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/AnimatableProperties/AnimatableProperties.html)

     

  6. ios 提取html 字符串中的img 的地址(图片地址)

    本文原文地址 http://www.cnblogs.com/qianLL/p/6082287.html 有时候 后台返回的是一串html'字符串 我们需要把里面的图片地址提取出来  这个关键就是一个正 ...

  7. java入门知识点结构

    第一部分    计算机程序和面向对象编程 编程语言种类: 机器语言:2进制(0和1) 汇编语言:英文字符缩写和助记符 高级语言: 面向过程:面向过程是从微观上/细节上处理具体事务. C语言 面向对象: ...

  8. svn报错:“Previous operation has not finished; run 'cleanup' if it was interrupted“ 的解决方法

    今天改完代码提交时,提交接近完成但窗口还未关闭电脑蓝屏了.夏天来了,电脑比人还怕热啊~~~   心里咯噔一下,估计svn又会出一些莫名其妙的问题了. 果然,待电脑重启后开eclipse,文件还是新增状 ...

  9. ORA-02292: integrity constraint (xxxx) violated - child record found

    在更新表的主键字段或DELETE数据时,如果遇到ORA-02292: integrity constraint (xxxx) violated - child record found 这个是因为主外 ...

  10. MySQL Performance-Schema(一) 配置篇

    performance-schema最早在MYSQL 5.5中出现,而现在5.6,5.7中performance-Schema又添加了更多的监控项,统计信息也更丰富,越来越有ORACLE-AWR统计信 ...