简体   繁体   English

输入类型编号的ng-pattern问题

[英]ng-pattern issue with input type number

I am facing one weird issue when trying to use ng-pattern with input type number. 在尝试使用带input type number. ng-pattern时,我遇到了一个奇怪的问题input type number.

<input type="number" name="input" min="0" max="1000" data-ng-model="input" ng-pattern="/^[0-9]*$/">
    <div data-ng-if="Form.input.$error.pattern">Please enter an Integer number between 0 and 1000.</div>
</div>

As per my regular expression defined, error should be shown when user input 1. . 根据我定义的正则表达式,当用户输入1.时应显示错误。 Right? 对?

However, it does not show error when type 1. but shows error for input 1.1 Why not for 1. ? 但是,它在类型1.时不显示错误1.但显示输入错误1.1为什么不为1.

Anything im missing here? 这里有什么遗失?

To allow numbers from 0 to 190 (including 190.0000) you can use type="text" and then use the following regex: 要允许0到190之间的数字(包括190.0000),您可以使用type="text"然后使用以下正则表达式:

^(?:(?:\d{1,2}|1[0-8]\d)(?:\.\d+)?|190(?:\.0+)?)$

See demo 演示

  • ^ - The beginning of string anchor (only match the rest if it appears right at the beginning of string, else fail the match) ^ - 字符串锚点的开头(如果它出现在字符串的开头,则仅匹配其余部分,否则将使匹配失败)
  • (?:(?:\\d{1,2}|1[0-8]\\d)(?:\\.\\d+)?|190(?:\\.0+)?) - a non-capturing group that is just used for grouping and not remembering the captured text. (?:(?:\\d{1,2}|1[0-8]\\d)(?:\\.\\d+)?|190(?:\\.0+)?) - 非捕获组这只是用于分组而不记住捕获的文本。 It contains 2 alternatives: 它包含2个替代方案:
    • (?:\\d{1,2}|1[0-8]\\d)(?:\\.\\d+)? :
      • (?:\\d{1,2}|1[0-8]\\d) - Either 1 or 2 any digits ( \\d{1,2} ) or 1 followed by 0 , 1 ... 8 , and then followed by any 1 digit. (?:\\d{1,2}|1[0-8]\\d) -是1或2的任何数字( \\d{1,2}1 ,接着01 ... 8 ,然后后跟任意1位数。
      • (?:\\.\\d+)? - optionally (as ? means match 1 or 0 times ) match a dot and 1 or more digits. - 可选(因为?表示匹配1或0次 )匹配一个点和一个或多个数字。
    • 190(?:\\.0+)? - matches 190 and then optionally . - 匹配190然后可选. and 1 or more 0 characters. 和1个或多个0字符。
  • $ - assert the end of string (if there are more characters after the preceding pattern, no match is found). $ - 断言字符串的结尾(如果前面的模式后面有更多的字符,则找不到匹配项)。

The non-capturing group can be replaced with a capturing group in your case without any visible change in the efficiency and behavior. 在您的情况下, 非捕获组可以替换为捕获组,而不会在效率和行为方面发生任何明显变化。 However, capturing groups keep the text they capture in some buffer, thus are supposed to be slower (though it is really insignificant). 然而,捕获组将它们捕获的文本保留在某个缓冲区中,因此应该更慢(尽管它实际上是无关紧要的)。 Also, capturing groups are necessary when back-references are used in the pattern (when you want to match already matched substrings), but it is not the case here. 此外,在模式中使用反向引用时(当您想匹配已经匹配的子字符串时),捕获组是必要的,但这不是这种情况。

Your regex works perfectly for me. 你的正则表达式对我来说非常合适。

<input type="number" name="input" min="0" max="1000" data-ng-model="input" ng-pattern="/^[0-9]*$/">
    <div data-ng-if="Form.input.$error.pattern">Please enter an Integer number between 0 and 1000.</div>
</div>

Here is the demo link: http://plnkr.co/edit/HV2PAPP2gh6cH8KXyYav?p=preview 以下是演示链接: http//plnkr.co/edit/HV2PAPP2gh6cH8KXyYav?p = preview

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

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