JQuery笔记(表单验证)_第1页
JQuery笔记(表单验证)_第2页
JQuery笔记(表单验证)_第3页
JQuery笔记(表单验证)_第4页
JQuery笔记(表单验证)_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、JQuery笔记(表单验证)jquery.validate.js 是 jquery 旗下的一个验证插件,借助 jquery 的优势,我们可以迅速验证一些常见的输入 , 并且可以自己扩充自己的验证方法。 举个例子,有这么一个表单: view plaincopy to clipboardprint?<form id="signupForm" method="get" action=""> <fieldset> <legend>Validating a complete form</legend&g

2、t; <p> <label for="firstname">Firstname</label> <input id="firstname" name="firstname" class="required"/> </p> <p> <label for="lastname">Lastname</label> <input id="lastname" name="las

3、tname" /> </p> <p> <label for="username">Username</label> <input id="username" name="username" /> </p> <p> <label for="password">Password</label> <input id="password" name="passwor

4、d" type="password" /> </p> <p> <label for="confirm_password">Confirm password</label> <input id="confirm_password" name="confirm_password" type="password" /> </p> <p> <label for="email"&g

5、t;Email</label> <input id="email" name="email" /> </p> <p> <input class="submit" type="submit" value="Submit"/> </p> </fieldset> </form> <form id="signupForm" method="get" action=

6、""><fieldset><legend>Validating a complete form</legend><p><label for="firstname">Firstname</label><input id="firstname" name="firstname" class="required"/></p><p><label for="lastname&q

7、uot;>Lastname</label><input id="lastname" name="lastname" /></p><p><label for="username">Username</label><input id="username" name="username" /></p><p><label for="password">Pass

8、word</label><input id="password" name="password" type="password" /></p><p><label for="confirm_password">Confirm password</label><input id="confirm_password" name="confirm_password" type="password&

9、quot; /></p><p><label for="email">Email</label><input id="email" name="email" /></p><p><input class="submit" type="submit" value="Submit"/></p></fieldset></form>在这个表单中,有名、

10、姓、用户名、密码、确认密码和 email 。他们都为非空,并且电子邮件需要是格式正确的地址、确认密码和密码一致。使用 jQuery 验证最简单的方式是引入 jquery.js 和 jquery validation.js 两个 js 文件。然后分别在 input 中加入 class 即: view plaincopy to clipboardprint?<input id="firstname" name="firstname" class="required"/> <input id="lastname&

11、quot; name="lastname" class="required"/> <input id="username" name="username" class="required"/> <input id="password" name="password" type="password" class="required"/> <input id="confirm

12、_password" name="confirm_password" type="password" class="required" equalTo="#password"/> <input id="email" name="email" class="required email"/> <input id="firstname" name="firstname" class=&q

13、uot;required"/><input id="lastname" name="lastname" class="required"/><input id="username" name="username" class="required"/><input id="password" name="password" type="password" class=&qu

14、ot;required"/><input id="confirm_password" name="confirm_password" type="password" class="required" equalTo="#password"/><input id="email" name="email" class="required email"/>以下列出 validate 自带的默认验证 re

15、quired: " 必选字段 ", remote: " 请修正该字段 ", email: " 请输入正确格式的电子邮件 ", url: " 请输入合法的网址 ", date: " 请输入合法的日期 ", dateISO: " 请输入合法的日期 (ISO).", number: " 请输入合法的数字 ", digits: " 只能输入整数 ", creditcard: " 请输入合法的信用卡号 ", equalTo:

16、" 请再次输入相同的值 ", accept: " 请输入拥有合法后缀名的字符串 ", maxlength: jQuery.format(" 请输入一个长度最多是 0 的字符串 "), minlength: jQuery.format(" 请输入一个长度最少是 0 的字符串 "), rangelength: jQuery.format(" 请输入一个长度介于 0 和 1 之间的字符串 "), range: jQuery.format(" 请输入一个介于 0 和 1 之间的值 "

17、), max: jQuery.format(" 请输入一个最大为 0 的值 "), min: jQuery.format(" 请输入一个最小为 0 的值 ") 然后,在 document 的 read 事件中,加入如下方法: <script> $(document).ready(function() $("#signupForm").validate(); </script> 这样,当 form 被提交的时候,就会根据 input 指定的 class 来进行验证了。如果失败, form 的提交就会被阻止。并且,

18、将提示信息显示在 input 的后面。 不过,这样感觉不好,因为验证规则侵入了我们的 html 代码。还有一个方式,便是使用“ rules” 。我们将 input 的那些验证用 class 删除掉。然后修改 document 的 ready 事件响应代码: $(document).ready(function() $("#signupForm").validate( rules: firstname:"required", lastname:"required", username:"required", pass

19、word:"required", confirm_password: required:true, equalTo:"#password" , email: required:true, email:true ); ) 这样以来,也能达到相同的效果。 那么,接下的问题,就是显示的错误提示是默认的。我们需要使用自定义的提示: $(document).ready(function() $("#signupForm").validate( rules: firstname:"required", lastname:&qu

20、ot;required", username:"required", password:"required", confirm_password: required:true, equalTo:"#password" , email: required:true, email:true , messages: firstname:" 必填项", lastname:" 必填项", username:" 必填项", password:" 必填项",

21、confirm_password: required:" 必填项", equalTo:" 密码不一致" , email: required:" 必填项", email:" 格式有误" ); ) 如果你还想在错误信息上显示特别的样式 ( 比如字体为红色 ) 即可通过添加: <style type="text/css"> #signupForm label.error padding-left: 16px; margin-left: 2px; color:red; background:

22、 url(img/unchecked.gif) no-repeat 0px 0px; </style> 继续添加对输入密码长度的验证规则: $(document).ready(function() $("#signupForm").validate( rules: firstname:"required", lastname:"required", username:"required", password: required:true, minlength:4, maxlength:15 , conf

23、irm_password: required:true, equalTo:"#password" , email: required:true, email:true , messages: firstname:" 必填项", lastname:" 必填项", username:" 必填项", password: required:" 必填项", minlength:jQuery.format(" 密码长度不少于 0 位 "), maxlength:jQuery.format

24、(" 密码长度不超过 0 位 ") , confirm_password: required:" 必填项", equalTo:" 密码不一致" , email: required:" 必填项", email:" 格式有误" ); ) 使用remote 可以通过 event 指定触发效验方式( 可选值有 keyup( 每次按键时 ) , blur( 当控件失去焦点时 ) ,不指定时就只在按提交按钮时触发 ) $(document).ready(function() $("#signupF

25、orm").validate( event:"keyup" | "blur" ) ) 如果通过指定 debug 为 true 则表单不会提交只能用来验证 ( 默认为提交 ) ,可用来调试 $(document).ready(function() $("#signupForm").validate( debug:true ) ) 如果在提交前还需要进行一些自定义处理使用 submitHandler 参数 $(document).ready(function() $("#signupForm").valida

26、te( SubmitHandler:function() alert( "success"); ) ) JQuery笔记(表单验证) 二 收藏 验证: <! DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN" "/TR/html4/loose.dtd" ><html> <head> <!-<style>label.valid background: url(' no-repeat

27、; height:16px; width:16px; display: block; position: absolute; top: 4px; left: 152px; </style>-> <script src=" <script type="text/javascript" src=" <script type="text/javascript" src=" <script type="text/javascript"> jQuery.validat

28、or.setDefaults( debug : true , success : "valid" ); </script> <script> $(document).ready( function () / 1 $( "#myform" ).validate( rules : fruit : "required" , errorPlacement : function (error, element) /if ( element.is(":radio") ) error.appendTo(e

29、lement.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

30、 ).rules( "add" , required : true , minlength : 2, messages : required : "Required input 必填 " , minlength : jQuery.format( "Please, at least 0 characters are necessary" ) ) ); );</script> </head> <body> <form id="myform"> <table bord

31、er="1"> <tr> <td> <label for="fruit"> Please select a fruit </label> </td> <td> <select id="fruit" name="fruit" title="Please select something!"> <option value=""></option> <option

32、 value="1"> Banana </option> <option value="2"> Apple </option> <option value="3"> Peach </option> </select> </td> </tr> <tr> <td> <input type="text" id="myinput" /> </td> <

33、;td> <input type="submit" value="submit!" /> </td> </tr> <tr> <td> <input type="radio" name="radio3"> radio3 <input type="radio" name="radio3"> radio3 </td> </tr> <tr> <td>

34、; <input type="radio" name="radio2"> radio2 <input type="radio" name="radio2"> radio2 </td> </tr> <tr> <td> <input type="radio" name="radio1"> radio1 <input type="radio" name="radi

35、o1"> radio1 </td> </tr> </table> </form> </body></html>参考:rules( ) Returns: Options Return the validations rules for the first selected element. rules( "add", rules ) Returns: Options JQuery笔记(表单验证) 三 收藏 Test for jQuery validate() plugin < typ

36、e="text/javascript"> jQuery Validation Plugin DemoLogin FormUsername Password 回到主页query-plugin-validation: |下载 |Changelog |Demos |Documentation 点击Login后 如下图: 代码 errorcontainer-demo_1.htmlview plaincopy to clipboardprint?<?xml version="1.0" encoding="ISO-8859-1" ?&

37、gt; <!DOCTYPE html PUBLIC "-/W3C/DTD XHTML 1.0 Transitional/EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ut

38、f-8" /> <title>Test for jQuery validate() plugin</title> <link rel="stylesheet" type="text/css" media="screen" href="css/screen.css" mce_href="css/screen.css" /> <mce:script src=" mce_src=" <mce:script type=&

39、quot;text/javascript" src=" mce_src=" <mce:script type="text/javascript" src=" mce_src=" <mce:script type="text/javascript"src="http:/jquery.bassistance.de/validate/lib/jquery.metadata.js"></mce:script> <mce:style type="te

40、xt/css"><!- /* .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.cmxf

41、orm width: 30em; form.cmxform label.error display: block; margin-left: 1em; width: auto; -></mce:style><style type="text/css" mce_bogus="1">/* .cmxform fieldset p.error label color: red; div.container background-color: #eee; border: 1px solid red; margin: 5px; padd

42、ing: 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; </style> <mce:script type="text/javascript&qu

43、ot;><!- / only for demo purposes $.validator.setDefaults( submitHandler: function() alert("submitted! (skipping validation for cancel button)"); ); $().ready(function() $("#form1").validate( errorLabelContainer: $("#form1 div.error") ); /* var container = $('d

44、iv.container'); / validate the form when it is submitted var validator = $("#form2").validate( errorContainer: container, errorLabelContainer: $("ol", container), wrapper: 'li', meta: "validate" ); */ $(".cancel").click(function() validator.resetFo

45、rm(); ); ); / -></mce:script> </head> <body> <h1 id="banner"><a href="http:/bassistance.de/jquery-plugins/jquery-plugin-validation/" mce_href="http:/bassistance.de/jquery-plugins/jquery-plugin-validation/">jQuery Validation Plugin</a&

46、gt; Demo</h1> <div id="main"> <form method="get" class="cmxform" id="form1" action=""> <fieldset> <legend>Login Form</legend> <p> <label>Username</label> <input name="user" title=&quo

47、t;Please enter your username (at least 3 characters)" class="required:true,minlength:3" /> </p> <p> <label>Password</label> <input type="password" maxlength="12" name="password" title="Please enter your password, between

48、 5 and 12 characters" class="required:true,minlength:5" /> </p> <div class="error"> </div> <p> <input class="submit" type="submit" value="Login"/> </p> </fieldset> </form> <!- our error conta

49、iner -> <!-<div class="container"> <h4>There are serious errors in your form submission, please see below for details.</h4> <ol> <li><label for="email" class="error">Please enter your email address</label></li> <l

50、i><label for="phone" class="error">Please enter your phone <b>number</b> (between 2 and 8 characters)</label></li> <li><label for="address" class="error">Please enter your address (at least 3 characters)</label&g

51、t;</li> <li><label for="avatar" class="error">Please select an image (png, jpg, jpeg, gif)</label></li> <li><label for="cv" class="error">Please select a document (doc, docx, txt, pdf)</label></li> </ol&

52、gt; </div>-> <!-<form class="cmxform" id="form2" method="get" action=""> <fieldset> <legend>Validating a complete form</legend> <p> <label for="email">Email</label> <input id="email"

53、name="email" class="validate:required:true,email:true" /> </p> <p> <label for="agree">Favorite Color</label> <select id="color" name="color" title="Please select your favorite color!" class="validate:requi

54、red:true"> <option></option> <option>Red</option> <option>Blue</option> <option>Yellow</option> </select> </p> <p> <label for="phone">Phone</label> <input id="phone" name="phone" class="some styles validate:required:true,number:true, rangelength:2,8" /> </p> <p> <label for="address">Address</label> <input id="address" name="address" class="some other styles validate:require

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论