admin管理员组文章数量:1356304
I am struggling writing a Regex for angular ng-pattern to validate a form input field.An example of the possible inputs are below
Valid input:
@tag1,@tag2,@tag3
@tag1, @tag2, @tag3
@tag1
Invalid Input:
@tag 1
@tag 1, @tag2, @tag -3
@ta g1, @t ag2
Basically it should allow ma and ma whitespace, but not allow whitespace in between tags.
I have written a REGEX that matches all the invalid input, but when I use this in ng-pattern it does not do the correct validation.
Regex Built: /((^, @|@\s+)|@*[a-zA-Z0-9]* +[a-zA-Z0-9])/g
Link:
Any help is much appreciated!
I am struggling writing a Regex for angular ng-pattern to validate a form input field.An example of the possible inputs are below
Valid input:
@tag1,@tag2,@tag3
@tag1, @tag2, @tag3
@tag1
Invalid Input:
@tag 1
@tag 1, @tag2, @tag -3
@ta g1, @t ag2
Basically it should allow ma and ma whitespace, but not allow whitespace in between tags.
I have written a REGEX that matches all the invalid input, but when I use this in ng-pattern it does not do the correct validation.
Regex Built: /((^, @|@\s+)|@*[a-zA-Z0-9]* +[a-zA-Z0-9])/g
Link: https://regex101./r/HMVdLD/1
Any help is much appreciated!
Share Improve this question asked Apr 13, 2017 at 12:35 Steven.linSteven.lin 1111 gold badge4 silver badges11 bronze badges 3- 3 Why use a regex that matches invalid input? – Wiktor Stribiżew Commented Apr 13, 2017 at 12:37
- I tried negating the above expression. That does not seem to work either. – Steven.lin Commented Apr 13, 2017 at 12:41
-
2
Look,
/^@[a-zA-Z0-9]+(?:,\s*@[a-zA-Z0-9]+)*$/
should match all valid ones, and it is much easier. Put it into theng-pattern
attribute value (ng-pattern="/pattern/"
). What about leading/trailing whitespace? Do you want it to be allowed in your input field? – Wiktor Stribiżew Commented Apr 13, 2017 at 12:44
1 Answer
Reset to default 6I suggest defining a pattern to match valid inputs, like
ng-pattern="/^@[a-zA-Z0-9]+(?:,\s*@[a-zA-Z0-9]+)*$/"
See the regex demo
If you want to disallow leading and trailing whitespace, add ng-trim="false"
.
Pattern details:
^
- start of string@
- a literal@
symbol[a-zA-Z0-9]+
- 1+ alphanumerics(?:,\s*@[a-zA-Z0-9]+)*
- 0+ sequences of:,
- ma\s*
- 0+ whitespaces@[a-zA-Z0-9]+
- same as above
$
- end of string.
本文标签: javascriptAngularJs ngpattern regexStack Overflow
版权声明:本文标题:javascript - AngularJs ng-pattern regex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743968246a2570288.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论