jQuery Validation Plugin客户端表单验证插件(2)

插件新增的几个方法和选择器

.valid()方法

检查一个表单 或 选取的控件 是否全部通过校验,调用此方法之前,需要表单被初始化form.validate();
因为正常触发表单的校验是点击submit按钮,用此方法,就可以在自定义的触发事件中调用校验,而且可以针对单个控件校验
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Sets up validation for a form, then checks if the form is valid when clicking a button.</title>
<link rel="stylesheet" href="http://jquery.bassistance.de/validate/demo/site-demos.css">
<style>
 
  </style>
</head>
<body>
<form id="myform">
<form id="myform">
  <input type="text" name="name" required>
  <br>
  <button type="button">Validate!</button>
</form>
</form>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});
var form = $( "#myform" );
form.validate();
$( "button" ).click(function() {
  alert( "Valid: " + form.valid() );
});
</script>
</body>
</html>

.rules()方法

操作表单中控件的校验规则,可以读取read新增add删除remove
$( "#myinput" ).rules( "add", {
  minlength: 2
});

$( "#myinput" ).rules( "add", {
  required: true,
  minlength: 2,
  messages: {
    required: "Required input",
    minlength: jQuery.format("Please, at least {0} characters are necessary")
  }
});

$( "#myinput" ).rules( "remove", "min max" );

新增的selector

:blank Selector

选取没有输入内容的控件或者只有空格输入的控件,校验方式其实就是jQuery.trim(value).length == 0
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Finds input elements with no value or just whitespace.</title>
<link rel="stylesheet" href="http://jquery.bassistance.de/validate/demo/site-demos.css">
 
</head>
<body>
<form id="myform">
<div>Mouseover to see the value of each input</div>
<input value="" title='""'>
<input value="   " title='"   "'>
<input value="abc" title='"abc"'>
</form>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});
$( "input:blank" ).css( "background-color", "#bbbbff" );
</script>
</body>
</html>

:filled Selector

选取有输入的控件,校验相当于jQuery.trim(value).length > 0

:unchecked Selector


:checked Selector


都是选取checkbox的选择器,分别对应未选中和选中的checkbox
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Finds all input elements that are unchecked.</title>
<link rel="stylesheet" href="http://jquery.bassistance.de/validate/demo/site-demos.css">
<style>
 
div { color:red; }
 
        </style>
</head>
<body>
<form id="myform">
<form>
    <input type="checkbox" name="newsletter" checked="checked" value="Hourly" />
    <input type="checkbox" name="newsletter" value="Daily" />
    <input type="checkbox" name="newsletter" value="Weekly" />
    <input type="checkbox" name="newsletter" checked="checked" value="Monthly" />
    <input type="checkbox" name="newsletter" value="Yearly" />
  </form>
  <div></div>
</form>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});
function countUnchecked() {
  var n = $( "input:unchecked" ).length;
  $( "div" ).text(n + (n == 1 ? " is" : " are") + " unchecked!" );
}
countUnchecked();
$( ":checkbox" ).click( countUnchecked );
</script>
</body>
</html>

本人自己开发的jquery表单验证控件! 使用方便,样式美观,完全开源。好不好用了就知道了! 插件名称:jquery表单验证插件 V1.0 插件作者:zy8008 最后更新:2011-4-28 插件说明: datatype类型说明: --------------------------------------- *:必填写,可为任意字符 number:数字 zip:邮政编码 mobile:手机号 email:邮箱 ajax:异步远程验证,必须添加ajaxurl属性并将要验证的字段保留空 如: reg:正则验证,必须添加reg属性 如: checkbox:复选框验证 radiobox:单选框验证 前台添加属性说明: ----------------------------------------------- datatype:必须 数据验证类型,见上面说明 errmsg:可选 错误信息,验证失败的信息,如果不设置或为空,则验证框显示“验证失败!” tip:可选 未验证前的提示信息,如果不设置或为空,则隐藏提示框 allowblank:可选 值为“true|false”默认为true 是否允许为空,如果设为true则值为空的时候也能通过验证,对*,ajax,checkbox,radiobox,reg无效 reg: 正则表达式规则,只能用开datatype为reg的时候,否则无效 ajaxurl:异步远程验证url,只能用的datatype为ajax的时候,否则无效。将要验证的字段放在url最后,将留空 如test.asp?uname= 调用说明: ------------------------------------------------- $(document).ready(function() { $("#aspnetForm").valideform({isokhide:true,btnsubmit:"#bbb"})}); #aspnetForm:需要验证的表单的ID,你也可以用jquery选择器自已定义 isokhid:可选 值为“true|false”默认为false 当点击提交按钮的时候,如果通过验证的项目将隐藏验证提示框 btnsubmit:可选 指定一个绑定提交事件的控件ID,如一个普通按钮或一个超链接,表单中的submit依然有效 其它具体使用方法,请参阅demo文件!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值