Jquery.validata 使用

来源:互联网 发布:莫泊桑谁的译本好 知乎 编辑:程序博客网 时间:2024/05/22 15:27
 

添加js引用

添加jquery.js,jquery.validate1.js,jquery.metadata.js,jQuery.validate.message_cn.js,jquery.validate.ext.js文件的引用。注意顺序

 

 

添加一个错误提示的样式(可定义自己喜欢的),

label.error

        {

            background:url(images/error.png) no-repeat 0px 0px;

            color: Red;

            padding-left: 20px;

       }

注册验证的form

<script type="text/javascript">

        $(function(){

            jQuery("#<%=form1.ClientID %>").validate();

        })

</script>

使用方法

格式是json,注意绿色的地方

例子:

验证电话的:

<asp:TextBox ID="txtTel"runat="server" Style="width: 200px;"CssClass="{required:true,isPhone:true, messages:{required:'忘记输入电话了哦',isPhone:'电话号码不对哦'}}"></asp:TextBox>

 

详细使用,请浏览demo

验证类型:

内置验证方式:

required()

返回:Boolean

必填验证元素

required(dependency-expression)

返回:Boolean

必填元素依赖于表达式的结果

required(dependency-callback)

返回:Boolean

必填元素依赖于回调函数的结果

remote(url)

返回:Boolean

请求远程校验。url通常是一个远程调用方法

minlength(length)

返回:Boolean

设置最小长度

maxlength(length)

返回:Boolean

设置最大长度

rangelength(range)

返回:Boolean

设置一个长度范围[min,max]

min(value)

返回:Boolean

设置最大值

max(value)

返回:Boolean

设置最小值

email()

返回:Boolean

验证电子邮箱格式

range(range)

返回:Boolean

设置值的范围

url()

返回:Boolean

验证URL格式

date()

返回:Boolean

验证日期格式(类似30/30/2008的格式,不验证日期准确性只验证格式)

dateISO()

返回:Boolean

验证ISO类型的日期格式

dateDE()

返回:Boolean

验证德式的日期格式(29.04.1994 or 1.1.2006)

number()

返回:Boolean

验证十进制数字(包括小数的)

digits()

返回:Boolean

验证整数

creditcard()

返回:Boolean

验证信用卡号

accept(extension)

返回:Boolean

验证相同后缀名的字符串

equalTo(other)

返回:Boolean

验证两个输入框的内容是否相同

phoneUS()

返回:Boolean

验证美式的电话号码

 

扩展的验证方式:(如果不需要,可以不添加jquery.validate.ext.js文件

stringCheck()

返回:Boolean

只能包括中文字、英文字母、数字和下划线

isIdCardNo()

返回:Boolean

验证身份证号码

isMobile()

返回:Boolean

验证手机号码

isTel()

返回:Boolean

验证电话号码

isPhone()

返回:Boolean

验证联系电话(手机/电话皆可)

isZipCode()

返回:Boolean

验证邮政编码