简体   繁体   English

jquery验证引擎中整数范围的自定义验证规则

[英]custom validation rule for integer range in jquery validation engine

I am using JQuery Validation Engine library. 我正在使用JQuery Validation Engine库。 I am trying to create a custom validation either through Regex or custom method to validate the field input. 我试图通过Regexcustom method创建自定义验证来验证字段输入。

I would suggest this validation name as custom integer validation containing integer range or some integers separated by comma(s) or combination of both . 我建议将此验证名称作为custom integer validation containing integer range or some integers separated by comma(s) or combination of both

Here is the criteria: 以下是标准:

  1. can enter any integer. 可以输入任何整数。 For eg 89 例如89
  2. can enter any integer range. 可以输入任何整数范围。 For eg 12-18 or 15-18 but not 19-3 (range should between min to max) 例如12-1815-18但不是19-3 (range should between min to max)
  3. can enter any combination of above two inputs separated by comma(,) only with or without spaces between comma. 可以输入以逗号(,)分隔的上述两个输入的任意组合,只逗号之间有或没有空格。 For eg 1-6, 5, 8-13, 7 . 例如1-6, 5, 8-13, 7 But not -3-5 (not -3) or 3-7-, 10 (not 7-) 但不是 -3-5 (not -3)3-7-, 10 (not 7-)

I tried this custom rule: 我试过这个自定义规则:

"integerRangeComma": {                    
             "regex": /^(([0-9]+)([\,]([0-9]+))?|([\,]([0-9]+))?)$/,
             "alertText": "* Invalid floating decimal number"
          },

My intended purpose to do the above validation is: 我进行上述验证的目的是:

I have a list of total documents lets suppose 12 . 我有一个总文件列表,我们假设12 I created a text-box in the form to accept what document no, he likes to add in his profile. 我在表单中创建了一个文本框,以接受他喜欢在他的个人资料中添加的文件。 And the above criteria should be applied on that. 上述标准应适用于此。

My next question would be, I also must validate that numbers entered by user do not greater than 12 (my presumption) . 我的下一个问题是,我还必须验证用户输入的数字不大于12 (my presumption)

How can I achieve this either through creating my own custom rule or something else. 如何通过创建自己的自定义规则或其他方法来实现此目的。

Thanks in Advance. 提前致谢。

Doing maths with regex is always a pain, you should parse it and check the not greater than 12 condition with code. 用正则表达式做数学总是很痛苦,你应该解析它并用代码检查不超过12的条件。 However, it is possible for this simple example by using 但是,通过使用这个简单的例子是可能的

[0-9]|1[12]

Your combination for an input of a single integer or a range will then be 那么您输入单个整数或范围的组合将是

number(-number)?

and for one input, or two commaseparated ones is 对于一个输入,或两个commaseparated的是

input(\s*,\s*input)?

If you want more than two, use * for unlimited or {0,n} for limited repetion instead of the ? 如果您想要两个以上,请使用*表示无限制,或使用{0,n}表示有限的重复而不是? .

So together, you get 所以,你得到了

var integerRangeComma = {                    
     "regex": /^([0-9]|1[12])(-[0-9]|-1[12])?(\s*,\s*([0-9]|1[12])(-[0-9]|-1[12])?)?$/,
     "alertText": "a single page number until 12 or an interval in them, or two of those separated by a comma"
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM