验证<输入类型="数" />与AngularJS和模式/正则表达式 [英] Validate <input type="number" /> with AngularJS and Pattern/RegEx

查看:158
本文介绍了验证<输入类型="数" />与AngularJS和模式/正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的类型数量的输入,我想,以确保它仅接受数字。我可以做在服务器端这很好,但我AngularJS无法得到它的工作。

So I have a input of type number, and I want to make sure it only accepts a number. I can do this fine on the Server side, but with AngularJS I can not get it to work.

这里的code:

<input type="number" ng-pattern="/[0-9]+/" name="numOfFruits" ng-model="basket.numOfFruits" />

我怀疑这是与我提供 [0-9] + 基本上我只希望在该文本框中的数字模式,任何不言了数字0到9,我想输入无效。

I suspect this has something to do with the pattern I am supplying [0-9]+ basically I only want numbers in this text box, anything that is not made up of the numbers 0 to 9, I want the input invalid.

目前,我的输入字段见此 aa23423 作为有效输入!!

At the moment, my input field sees this aa23423 as valid input!!

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

感谢你。

推荐答案

您需要使用锚:

/^[0-9]+$/


  • ^ :开始的行锚

  • [0-9] + 0 之间的一个或多个数字和 9

  • $ :尾线锚

    • ^: Start-of-line anchor.
    • [0-9]+ One or more numbers between 0 and 9.
    • $: End-of-line anchor.
    • 所以这个字符串的开头匹配,那么它的一个或多个数字相匹配,在那之后它会寻找结束的字符串,因此它仅包含数字和没有别的字符串匹配。

      So this matches the start of the string, then it matches the one or more digits, after that it looks for the end-of-string, so it matches a string containing only numbers and nothing else.

      另外, / [0-9] + / 将匹配只有aa23423 的一部分,更准确地数 23423 ,因此会给你有效的。

      Otherwise, /[0-9]+/ will match only a part of aa23423, more accurately the number 23423 and thus will give you valid.

      这篇关于验证&LT;输入类型=&QUOT;数&QUOT; /&GT;与AngularJS和模式/正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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