[英]ng-pattern regex to match multiple conditions
I'm using ng-pattern to validate a field for the following conditions. 我正在使用ng-pattern验证以下条件的字段。 Thus far I'm only able to validate the last two conditions using the following regex.
到目前为止,我只能使用以下正则表达式来验证最后两个条件。 When I add only numbers, I'd like to validate the length as well.
当我仅添加数字时,我也想验证长度。
How can I validate all four conditions inside ng-patter? 如何验证ng-patter中的所有四个条件? Do I need to surround then with brackets separately?
我是否需要分别用括号包围?
data-ng-pattern="/^[0-9-\s()+]+$/"
11111111
111111111
11-111-111
111-111-111
I'd like to validate multiple conditions 我想验证多个条件
The problem with ^[1-9][0-9]{6}*$
is it is an invalid regex because of {6}*
and ^([^0][0-9]){6}$
is that it is allowing any character that is not 0
followed by six digits. ^[1-9][0-9]{6}*$
是它是无效的正则表达式,因为{6}*
和^([^0][0-9]){6}$
是它允许任何不为0
后跟六位数字的字符。
Use 采用
^[1-9][0-9]{5}$
Explanation: 说明:
^
: Starts with anchor ^
:从锚点开始 [1-9]
: Matches exactly one digit from 1 to 9 [1-9]
:从1到9精确匹配一位数字 [0-9]{5}
: Matches exactly five digits in the inclusive range 0-9
[0-9]{5}
:在0-9
范围内精确匹配五位数字 $
: Ends with anchor $
:以锚点结尾 Regex101 Playground Regex101游乐场
HTML5 Demo: HTML5演示:
Show code snippet 显示代码段
input:invalid { color: red; }
input:invalid { color: red; }
<input type="text" pattern="[1-9][0-9]{5}" />
Run code snippetHide results
input:invalid { color: red; }
<input type="text" pattern="[1-9][0-9]{5}" />
运行代码段隐藏结果
try this. 尝试这个。 it will only allow 10 digits.
只能输入10位数字。
/^[1-9]{1}[0-9]{9}$/
i'm not sure this would work but i tried. 我不确定这是否可行,但我尝试过。
/^[1-9]{1}[0-9-\s()+]{10}$/gm
Explainlation 讲解
1) ^ Beginning. 1)^开始。 Matches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled.
匹配字符串的开头,如果启用了多行标志(m),则匹配行的开头。
2) [ Character set. 2)[字符集。 Match any character in the set.
匹配集合中的任何字符。 1-9 Range.
1-9范围。 Matches a character in the range "1" to "9" (char code 49 to 57).
匹配范围从“ 1”到“ 9”(字符代码49到57)的字符。 ]
]
3) {1} Quantifier. 3){1}量词。 Match 1 of the preceding token.
匹配前一个令牌的1。
4) [ Character set. 4)[字符集。 Match any character in the set.
匹配集合中的任何字符。
5) 0-9 Range. 5)0-9范围。 Matches a character in the range "0" to "9" (char code 48 to 57).
匹配范围从“ 0”到“ 9”(字符代码48到57)的字符。
6) - Character. 6)-角色。 Matches a "-" character (char code 45).
匹配一个“-”字符(字符代码45)。
7) \\s Whitespace. 7)\\ s空白。 Matches any whitespace character (spaces, tabs, line breaks).
匹配任何空白字符(空格,制表符,换行符)。
8) ( Character. Matches a "(" character (char code 40). 8)(字符。匹配“(”字符(字符代码40)。
9) ) Character. 9))字符。 Matches a ")" character (char code 41).
匹配“)”字符(字符代码41)。
10) + Character. 10)+字符。 Matches a "+" character (char code 43).
匹配一个“ +”字符(字符代码43)。 ]
]
11) {10} Quantifier. 11){10}量词。 Match 10 of the preceding token.
匹配前一个令牌的10。
12) $ End. 12)$结束。 Matches the end of the string, or the end of a line if the multiline flag (m) is enabled.
匹配字符串的末尾,如果启用了多行标志(m),则匹配行的末尾。
13) g modifier: global. 13)g修饰符:全局。 All matches (don't return on first match)
所有比赛(在第一个比赛中不返回)
14) m modifier: multi-line. 14)m修饰符:多行。 Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
使^和$匹配每行的开始/结束(不仅是字符串的开始/结束)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.