简体   繁体   English

AngularJs文本输入字段正则表达式

[英]AngularJs text input field regular expression

Currently, I use the following expression for my ngPattern attribute: /[^*]/ meaning that any input is allowed but it has to contain at least one char different from *. 当前,我对ngPattern属性使用以下表达式: /[^*]/表示允许任何输入,但必须包含至少一个与*不同的字符。 Now, additional requirement arose - the input has to be numeric only. 现在,产生了其他要求-输入必须仅是数字。

Is there any possibility to combine these two requirements in one single regular expression? 有可能将这两个要求合并为一个正则表达式吗? I just can't get it... Or should I move the condition check to my controller, where I would apply two regular expressions consecutively, programmatically. 我只是听不到它...还是应该将条件检查移到我的控制器上,在这里我将以编程方式连续应用两个正则表达式。

Do you want the input to be numeric, but * is also allowed? 您是否希望输入为数字,但也允许*? If so, this will do the trick: 如果是这样,这将达到目的:

<input type="text" ng-pattern="[1-9\*]*">

In general, you can do most stuff in regex, so there is no need to take it to the controller. 通常,您可以在正则表达式中执行大多数操作,因此无需将其带到控制器。

OK, I asked a colleague of mine today. 好,我今天问我的一位同事。 Here is the right solution: /[\\d*]*\\d[\\d*]*/ 这是正确的解决方案: /[\\d*]*\\d[\\d*]*/

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

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