转自:http://www.jb51.net/article/76595.htm

jQuery Validate插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证,同时提供了一个用来编写用户自定义方法的 API。所有的捆绑方法默认使用英语作为错误信息,且已翻译成其他 37 种语言。

第一节:jQuery Validation让验证变得如此容易

一、官网下载jquery,和jquery validation plugin 
二、引入文件

<script src="js/jquery-1.8.0.min.js" type="text/javascript"></script> 
<script src="js/jquery.validate.js" type="text/javascript"></script>
三、接下来,声明下面的HTML片段
<form action="" id="jvForm">
 姓名:<input type="text" name="username" id="username" class="required" /></br>
 密码:<input type="password" name="password" id="password" class="required"/></br>
 <input type="submit" value="提交" />
</form>
通过以上代码,大家会发现我们对于每一个input都加上了class="required" 
他的作用就是在这个inpute标签为空时会提示用户出错。
四、最后我们要为我们的框架找到一个切入点
<script type="text/javascript">
 $(function() {
  $("#jvForm").validate();
 })
</script>
运行效果:
当然提示信息默认是英文的,可根据需要在jquery.validate.js里修改。
 

第二节:jQuery Validation让验证变得如此容易

上一个例子我们是统一引用jquery.validate.js这样所有必填字段的提示信息都将是This field is required.
现在要改成动态提示,比如姓名如果为空则提示姓名不能为空,密码如果为空则提示密码不能为空。
这次我们将校验规则写在代码里
首先还是先引入文件

<script src="js/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
接下来,声明下面的HTML片段
<form action="" id="jvForm">
 姓名:<input type="text" name="username" id="username" /></br>
 密码:<input type="password" name="password" id="password" /></br>
 <input type="submit" value="提交" />
</form>
和之前的相比没有了class="required"
最后 校验规则如下:
$(function() {
 $("#jvForm").validate({
  rules: {
   username: {
    required: true
   },
   password: {
    required: true
   }
  },
  messages: {
   username: {
    required: "姓名不能为空!"
   },
   password: {
    required: "密码不能为空!"
   }
  }
 });
})
 运行效果:

第三节:jQuery Validation让验证变得如此容易

以下代码进行对jQuery Validation的简单演示包括必填项、字符长度,格式验证

一、引入文件

<script src="js/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
二、声明HTML片段
<form action="" id="jvForm">
  用 户 名:<input type="text" name="username"/></br>
  密 码:<input type="password" name="password" id="password"/></br>
  确认密码:<input type="password" name="confirm_password"/></br>
  出 生 地:<select name="address"><option value="">--</option><option value="1">北京</option>
 <option value="1">上海</option><option value="1">深圳</option></select></br>
  手 机:<input type="text" name="mobile" /></br>
  邮 箱:<input type="text" name="email" /></br>
  <input type="submit" value="提交" />
</form>
三、错误提示样式
<style type="text/css">
 label.error{font-size:12px;font-weight: normal;color:#ff0511;margin-left:10px;}
</style>
四、验证代码
<script type = "text/javascript">
$(function() {
 $("#jvForm").validate({
  rules: {
   username: { //用户名必填 至少3位
    required: true,
    minlength: 3
   },
   password: { //密码必填 至少6位
    required: true,
    minlength: 6
   },
   confirm_password: { //密码确认
    required: true,
    equalTo: "#password"
   },
   address: { //出生地必填
    required: true
   },
   mobile: { //手机必填 验证格式
    required: true,
    mobile: true
   },
   email: { //email必填 验证格式
    required: true,
    email: true
   },
  
  },
  messages: {
   username: {
    required: "用户名不能为空!",
    minlength: "用户名至少三位!"
   },
   password: {
    required: "密码不能为空!",
    minlength: "密码至少六位!"
   },
   confirm_password: {
    required: "密码确认不能为空!",
    equalTo: "两次输入密码不一致 !"
   },
   address: {
    required: "请选择出生地!",
   },
   mobile: {
    required: "手机不能为空!",
    mobile: "手机格式不正确",
   },
   email: {
    required: "邮箱不能为空!",
    email: "邮箱格式不正确",
   },
  }
 });
})
</script>
运行效果:
 

jQuery Validate插件实现表单强大的验证功能的更多相关文章

  1. jQuery Validate 插件为表单提供了强大的验证功能

    之前项目开发中,表单校验用的jQuery Validate 插件,这个插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的 ...

  2. validate插件实现表单效验(二)

    一款优秀的表单验证插件——validation插件 特点: l  内置验证规则:拥有必填.数字.email.url和信用卡号码等19类内置验证规则 l  自定义验证规则:可以很方便的自定义验证规则 l ...

  3. jquery validate 一个注册表单的应用

    先看页面 前端表单代码  register.html <form class="mui-input-group" id="regForm"> < ...

  4. 表单提交学习笔记(二)—使用jquery.validate.js进行表单验证

    一.官网下载地址:http://plugins.jquery.com/validate/ 二.用法 1.在页面上进行引用 <script src="~/scripts/jquery-1 ...

  5. jquery.validate.js自定义表单验证

    $(document).ready(function() { //在下列位置输入页面加载的逻辑代码 $("#inputForm").validate({ rules: { seq: ...

  6. validate插件实现表单效验(一)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. [转]jQuery.validate插件在失去焦点时执行验证代码

    转:http://my.oschina.net/enyo/blog/311566 关于 jquery.validate.js 表单验证插件如何在失去焦点时做验证.看手册后发现默认是在表单提交时执行验证 ...

  8. jQuery html5Validate基于HTML5表单 异步服务器端验证

    1. HTML5 自带的Validate 很漂亮,很好用, 但是一定要在form里用submit按钮,才生效 <form id="frmInfo" action=" ...

  9. jquery.form插件 提交表单 type="hidden"取不到值的问题记录

    1.外国文献:说可以改成其他的(非hidden),再加style="display:none"隐藏. <INPUT type="password" sty ...

随机推荐

  1. Extjs查询实现

    效果图如上: 页面代码: Ext.QuickTips.init(); //放在图标上会自动提示信息 Ext.define('ExtApp.view.StudentList' , { extend : ...

  2. ZOJ - 3992 - One-Dimensional Maze (思维)

    题意: 一条长度为n的直线,你一开始在位置m上 其中每个整点都有一个字符'L'或'R',如果是'L'那么你必须往左走一步,否则往右走一步 如果你到达位置1或位置n你任务就完成了 不过有可能你永远到不了 ...

  3. [luogu4571 JSOI2009] 瓶子和燃料 (数论)

    传送门 Solution 题目说的很迷,但可以发现两个瓶子互相倒最少是容积的gcd 那么题目就转化为求其中选k个瓶子gcd的最大值,这个可以分解因数,枚举因数得到 Code //By Menteur_ ...

  4. Uva 10730 Antiarithmetic?

    uva 10730 题意:给出一列数字,如果其中存在长度大于等于3的等差数列,输出no,不存在就输出yes 这道题标定了数列长度n,而且这n个数数据范围是从0到n-1,没有相同的数,这就给我们枚举提供 ...

  5. HDU 4923 (贪心+证明)

    Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is ...

  6. [Usaco2015 dec]Max Flow

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 204  Solved: 129[Submit][Status][Discuss] Descriptio ...

  7. tyvj3737 逐个击破

    描述 三大战役的平津战场上,傅作义集团在以北平.天津为中心,东起唐山西至张家口的铁路线上摆起子一字长蛇阵,并企图在溃败时从海上南逃或向西逃窜.为了就地歼敌不让其逃走,mzd制定了先切断敌人东洒两头退路 ...

  8. 5-46 新浪微博热门话题 (30分)——unfinished HASH

    5-46 新浪微博热门话题   (30分) 新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题.新浪微 ...

  9. Windows下Redis的安装与部署

    1.下载地址:https://github.com/MSOpenTech/redis/releases 2.下载zip的包,下载后放到需要安装的目录进行解压操作,列如:F:\Redis\Redis-x ...

  10. open redis port for remote connections

    edit /etc/redis.conf Add below line after bind 127.0.0.1, then try redis-cli -h xxx.xxx.xxx.xxx ping ...