我应该使用内置的类还是规则来实现我的验证? [英] Should I implement my validation using the built-in class or the rule?

查看:88
本文介绍了我应该使用内置的类还是规则来实现我的验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery验证插件验证我的表单。正如我所看到的那样(我很难从文档中找出东西),我有两个基本的选择:如何应用一个内置的验证规则:

I am using the jQuery validate plugin to validate my form. As I see it (I'm having a hard time figuring things out from the documentation), I have 2 basic choices of how to apply a built-in validation rule:


  1. 在HTML中应用该类

< input name = myNumid =myNumtype =textclass =number/>

调用验证方法时输入id的规则

$('#sales').validate({
    rules: {
        myNum: {number: true}
    }
});


哪个被认为是最佳实践?我试图决定是否更清楚地在一个地方验证验证方法中的所有规则,以便我可以在一个集中的位置看到我所有的验证,或者如果在HTML中编写简单的规则是有意义的类并且仅在验证方法中进行自定义验证。什么是更接受的编码验证方式?使用哪种方法应该确定什么?

Which is considered "best-practice"? I'm trying to decide if it's clearer to just code all the rules in one place in the validate method, so that I can see all my validation in one centralized location, or if it makes sense to code the simple rules in the HTML as classes and only put customized validation in the validate method. What is the more accepted way of coding the validation? What should determine when I use which method?

推荐答案

除了需要考虑的个人选择之外,唯一的因素是,如果要作为规则应用的方法是需要一个附加参数的方法,那么您不能将方法名称用作类名。例如这些方法

The only factor other than personal choice that you need to consider is that if the method that you want applied as a rule is one that takes an additional parameter, then you cannot use the method name as a classname. For instance these methods


  • minlength,

  • maxlength

  • rangelength


  • max

  • 范围

  • 等于

  • minlength,
  • maxlength
  • rangelength
  • min
  • max
  • range
  • equalTo

所有参数 - 即他们的定义是

all take a parameter - ie their definition is

function(value, element, param)

而不是

function(value, element)

所以这个标记不起作用

<!-- doesn't work --> 
<input name="email2" class="minlength"/>

不需要额外参数的方法是必需,电子邮件,url ,'date'等。

Methods that don't take an extra parameter are 'required', 'email', 'url', 'date' etc.

所以即使你自己的自定义方法,你可以将它们应用到一个表单字段的classname,只要他们有一个签名函数(值,元素)

So even for your own custom methods you can apply them to a form field by classname, as long as they have a signature function(value, element)

文档不完全是最新的 - 有多种方法将方法应用于标签中的输入字段,包括文档中没有提到的数据 - 属性。例如这些都是有效的:

The docs are not fully up to date - there are multiple ways to apply a method to an input field in markup, including the data- attributes which are not mentioned in the documentation at all. For example these are all valid:

<input name="email1" required />
<input name="email2" class="required"/>
<input name="email3" type="required"/>
<input name="email4" data-rule-required="true" />
<input name="email5" data-rule-required="true" data-rule-minlength="5" />
<input name="email6" data-rule-required="true" data-rule-range="[1,5]"/>

nb。对于数据风格,您需要最新版本的插件

nb. for the data- style you need an up to date version of the plugin

这篇关于我应该使用内置的类还是规则来实现我的验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆