[译] HTML5 data 属性规则使用 jQuery Validate 插件

栏目: Html5 · 发布时间: 6年前

内容简介:原文地址:我想我刚开始知道这个特性是因为ASP.NET MVC 使用了jQuery Validate 的

原文地址: Using the jQuery Validate Plugin with HTML5 Data Attribute Rules

The jQuery Validation Plugin 是一个非常非常好用的插件. 和 ASP.NET MVC uses the plugin for client side validation 一样好用! 他有个非常好用的 JavaScript API , 对于写验证规则或者信息验证来说. 具体内容可以查看documentation , 然而文档中没有完全介绍的特性就是: 使用 html5 数据属性

我想我刚开始知道这个特性是因为ASP.NET MVC 使用了jQuery Validate 的 无感验证 验证规则. 意思是不用在你的标签中输入行内 javascript, 替代方法就是 使用 html data 属性. 显然你可以在 1.11.0 . 之后使用任何的数据验证规则.

基本示例

如果你对这个没有概念, 在 JS Fiddle 上有个简单的示例访问 JS Fiddle.

这里是代码:

<!DOCTYPE html>
<html>
    <form id="validate-me-plz">
        <div>
            Required: <input type="text" name="firstName" data-rule-required="true" />
        </div>
        <div>
            <input type="submit" value="Submit" />
        </div>
    </form>

    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>

    <script type="text/javascript">
        $('#validate-me-plz').validate();
    </script>
</html>
复制代码

你可以看到, 在输入框的时候, 我们有个 data-rule-required 属性设置为 true , 我们仅仅在最后调用 .validate() 方法. 这个验证方法会验证数据属性并且运营验证规则. 像之前提到的, 有一系列的验证规则可供验证

规则格式

添加如下的验证规则到input 元素中

data-rule-[rule name separate by dashes]="true" 
复制代码

如下示例:

data-rule-required="true"
data-rule-email="true"
data-rule-minlength="6"

提示信息格式

默认的 jQuery Validation 会添加自己的验证规则, 但是你也可以自定义自己的验证规则. 指定验证信息使用如下的规则

data-msg-[rule name separate by dashes]="The message you want."
复制代码

如下示例:

data-msg-required="Madam/sir, this field is required."
data-msg-email="Let us spam you, enter a valid email address."

完整示例:

这是在 JS Fiddle 上的一个更完整的示例, 示例项包含不同的验证规则和验证信息JS Fiddle.

完整代码:

<!DOCTYPE html>
<html>
    <form id="validate-me-plz">
        <div>
            Required: <input type="text" name="required" data-rule-required="true" />
        </div>
        <div>
            Required w/custom message: <input type="text" name="required-sassy" data-rule-required="true" data-msg-required="Please enter SOMETHING." />
        </div>
        <div>
            Email: <input type="text" name="email" data-rule-email="true"/>
        </div>
        <div>
            Email w/custom message: <input type="text" name="anotherEmail" data-rule-email="true" data-msg-email="Please enter a valid email address you dummy." />
        </div>
        <div>
            <input type="submit" value="Validate!" />
        </div>
    </form>

    <script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.js"></script>
    <script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>

    <script type="text/javascript">
        $('#validate-me-plz').validate();
    </script>
</html>
复制代码

怎样工作/起作用的

如果你对怎么起作用的比较关注查看 look at core.js around line 928 他简单的使用了 jQuery data() 方法来检测每个验证元素, 自动将data 属性中的验证属性转换为规则.

value = $element.data("rule" + method[ 0 ].toUpperCase() + method.substring( 1 ).toLowerCase());
复制代码

(ps)以下是作者对破折号的想法: But where are the dashes? I didn't realize it, but data attributes can (should?) be referenced via jQuery without their dashes. Instead of the dashes youCamel Case the data attribute name, without the "data-" prefix. The above code results in something like this for the required rule: value = $element.data("ruleRequired"); which maps to the data-rule-required attribute.

验证规则

如果你想知道哪些验证器可用, 需要访问 look at the code for the validators in core or browse the additional validators .

以下是从 GitHub 上搞到解析出来的代码, 我做了如下标注

框架新增

  • data-rule-mobile 验证手机号
  • data-rule-qq 验证QQ号码
  • data-rule-chId 身份证号
  • data-rule-decimal 输入小数
  • data-rule-noSpace 不允许存在空格
  • data-rule-phoneZh 固话
  • data-rule-phoneAmobile 固话/手机号

(Tested, core)

  • data-rule-required="true"
  • data-rule-email="true"

(Untested, core, but should work)

  • data-rule-url="true"
  • data-rule-date="true"
  • data-rule-dateISO="true"
  • data-rule-number="true"
  • data-rule-digits="true"
  • data-rule-creditcard="true"
  • data-rule-minlength="6"
  • data-rule-maxlength="24"
  • data-rule-rangelength="5,10"
  • data-rule-min="5"
  • data-rule-max="10"
  • data-rule-range="5,10"
  • data-rule-equalto="#password"
  • data-rule-remote="custom-validatation-endpoint.aspx"

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

百度SEM竞价推广

百度SEM竞价推广

马明泽 / 电子工业出版社 / 2017-5 / 59

竞价推广已成为企业昀主要的网络营销方式,《百度SEM竞价推广:策略、方法、技巧与实战》以百度竞价推广为基础,全面阐述了整个竞价推广过程中的重要环节,涉及大量账户操作实战技巧,以及解决各类难点的方法,其中包括搜索引擎营销基础、百度搜索推广介绍、账户结构搭建技巧、关键词与创意的使用技巧、质量度优化与提升、账户工具的使用、百度推广客户端的使用、企业搜索推广方案制作、百度网盟推广、着陆页分析、效果优化与数......一起来看看 《百度SEM竞价推广》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具