




已阅读5页,还剩31页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
jquery.validate.js 是 jquery 旗下的一个验证插件,借助 jquery 的优势,我们可以迅速验证一些常见的输入 , 并且可以自己扩充自己的验证方法。 举个例子,有这么一个表单: view plaincopy to clipboardprint? Validating a complete form Firstname Lastname Username Password Confirm password Email Validating a complete formFirstnameLastnameUsernamePasswordConfirm passwordEmail在这个表单中,有名、姓、用户名、密码、确认密码和 email 。他们都为非空,并且电子邮件需要是格式正确的地址、确认密码和密码一致。使用 jQuery 验证最简单的方式是引入 jquery.js 和 jquery validation.js 两个 js 文件。然后分别在 input 中加入 class 即: view plaincopy to clipboardprint? 以下列出 validate 自带的默认验证 required: 必选字段 , remote: 请修正该字段 , email: 请输入正确格式的电子邮件 , url: 请输入合法的网址 , date: 请输入合法的日期 , dateISO: 请输入合法的日期 (ISO)., number: 请输入合法的数字 , digits: 只能输入整数 , creditcard: 请输入合法的信用卡号 , equalTo: 请再次输入相同的值 , accept: 请输入拥有合法后缀名的字符串 , maxlength: jQuery.format( 请输入一个长度最多是 0 的字符串 ), minlength: jQuery.format( 请输入一个长度最少是 0 的字符串 ), rangelength: jQuery.format( 请输入一个长度介于 0 和 1 之间的字符串 ), range: jQuery.format( 请输入一个介于 0 和 1 之间的值 ), max: jQuery.format( 请输入一个最大为 0 的值 ), min: jQuery.format( 请输入一个最小为 0 的值 ) 然后,在 document 的 read 事件中,加入如下方法: $(document).ready(function() $(#signupForm).validate(); 这样,当 form 被提交的时候,就会根据 input 指定的 class 来进行验证了。如果失败, form 的提交就会被阻止。并且,将提示信息显示在 input 的后面。 不过,这样感觉不好,因为验证规则侵入了我们的 html 代码。还有一个方式,便是使用“ rules” 。我们将 input 的那些验证用 class 删除掉。然后修改 document 的 ready 事件响应代码: $(document).ready(function() $(#signupForm).validate( rules: firstname:required, lastname:required, username:required, password:required, confirm_password: required:true, equalTo:#password , email: required:true, email:true ); ) 这样以来,也能达到相同的效果。 那么,接下的问题,就是显示的错误提示是默认的。我们需要使用自定义的提示: $(document).ready(function() $(#signupForm).validate( rules: firstname:required, lastname:required, username:required, password:required, confirm_password: required:true, equalTo:#password , email: required:true, email:true , messages: firstname: 必填项, lastname: 必填项, username: 必填项, password: 必填项, confirm_password: required: 必填项, equalTo: 密码不一致 , email: required: 必填项, email: 格式有误 ); ) 如果你还想在错误信息上显示特别的样式 ( 比如字体为红色 ) 即可通过添加: #signupForm label.error padding-left: 16px; margin-left: 2px; color:red; background: url(img/unchecked.gif) no-repeat 0px 0px; 继续添加对输入密码长度的验证规则: $(document).ready(function() $(#signupForm).validate( rules: firstname:required, lastname:required, username:required, password: required:true, minlength:4, maxlength:15 , confirm_password: required:true, equalTo:#password , email: required:true, email:true , messages: firstname: 必填项, lastname: 必填项, username: 必填项, password: required: 必填项, minlength:jQuery.format( 密码长度不少于 0 位 ), maxlength:jQuery.format( 密码长度不超过 0 位 ) , confirm_password: required: 必填项, equalTo: 密码不一致 , email: required: 必填项, email: 格式有误 ); ) 使用remote 可以通过 event 指定触发效验方式( 可选值有 keyup( 每次按键时 ) , blur( 当控件失去焦点时 ) ,不指定时就只在按提交按钮时触发 ) $(document).ready(function() $(#signupForm).validate( event:keyup | blur ) ) 如果通过指定 debug 为 true 则表单不会提交只能用来验证 ( 默认为提交 ) ,可用来调试 $(document).ready(function() $(#signupForm).validate( debug:true ) ) 如果在提交前还需要进行一些自定义处理使用 submitHandler 参数 $(document).ready(function() $(#signupForm).validate( SubmitHandler:function() alert( success); ) ) JQuery笔记(表单验证) 二 收藏 验证: !-label.valid background: url(/view/trunk/plugins/validate/demo/images/checked.gif) no-repeat; height:16px; width:16px; display: block; position: absolute; top: 4px; left: 152px; - jQuery.validator.setDefaults( debug : true , success : valid ); $(document).ready( function () / 1 $( #myform ).validate( rules : fruit : required , errorPlacement : function (error, element) /if ( element.is(:radio) ) error.appendTo(element.parent(); ); / 2 $( #myinput ).rules( add , required : true , minlength : 2, messages : required : Required input , minlength : jQuery.format( Please, at least 0 characters are necessary ) ); / 3 $( :radio ).each( function () $( this ).rules( add , required : true , minlength : 2, messages : required : Required input 必填 , minlength : jQuery.format( Please, at least 0 characters are necessary ) ) ); ); Please select a fruit Banana Apple Peach radio3 radio3 radio2 radio2 radio1 radio1 参考:/Plugins/Validationrules( ) Returns: Options Return the validations rules for the first selected element. rules( add, rules ) Returns: Options JQuery笔记(表单验证) 三 收藏 Test for jQuery validate() plugin jQuery Validation Plugin DemoLogin FormUsername Password 回到主页query-plugin-validation: |下载 |Changelog |Demos |Documentation 点击Login后 如下图: 代码 errorcontainer-demo_1.htmlview plaincopy to clipboardprint? Test for jQuery validate() plugin /* .cmxform fieldset p.error label color: red; div.container background-color: #eee; border: 1px solid red; margin: 5px; padding: 5px; div.container ol li list-style-type: disc; margin-left: 20px; div.container display: none .container label.error display: inline; */ form.cmxform width: 30em; form.cmxform label.error display: block; margin-left: 1em; width: auto; jQuery Validation Plugin Demo Login Form Username Password !- There are serious errors in your form submission, please see below for details. Please enter your email address Please enter your phone number (between 2 and 8 characters) Please enter your address (at least 3 characters) Please select an image (png, jpg, jpeg, gif) Please select a document (doc, docx, txt,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年度浙江省二级造价工程师之建设工程造价管理基础知识测试卷(含答案)
- 2024年度浙江省二级造价工程师之建设工程造价管理基础知识强化训练试卷B卷附答案
- 中职学生心理健康教育课件
- 肿瘤消融治疗护理
- 行政年终个人工作总结
- 临床护士分层培训
- 痔疮的中医护理
- 全网营销课程培训
- 肿瘤科叙事护理实践体系
- 幼儿园小班美术教案制作红绿灯
- 供水加压泵站管理制度
- 保险公司保密管理制度
- 机动车交通事故责任纠纷民事起诉状(模板)
- 国家开放大学《药学信息检索》形考作业参考答案
- 社区卫生服务中心工作制度与人员岗位职责
- 大管棚施工记录表
- 筒仓使用安全管理规程
- 门诊病历的与处方书写规范课件
- 人教版小学五下数学第九单元:总复习教学计划
- 大学生选课申请表
- GB∕T 24202-2021 光缆增强用碳素钢丝
评论
0/150
提交评论