admin管理员组文章数量:1297112
In form - name field I want to do a validation against
- Should allow only Alpha characters
- Allow space
- Allow certain special characters -
_ \ / - . ’
I tried,
ng-pattern="/^[a-z]+[_+\+/+-+.+’][a-z]*$/"
Looks like I have missed out something as its not working!
In form - name field I want to do a validation against
- Should allow only Alpha characters
- Allow space
- Allow certain special characters -
_ \ / - . ’
I tried,
ng-pattern="/^[a-z]+[_+\+/+-+.+’][a-z]*$/"
Looks like I have missed out something as its not working!
Share Improve this question edited Dec 1, 2016 at 8:35 Tushar 87.2k21 gold badges163 silver badges181 bronze badges asked Dec 1, 2016 at 8:03 mondamonda 3,91516 gold badges61 silver badges85 bronze badges 02 Answers
Reset to default 6Let's see:
- Should allow only Alpha characters =>
[a-zA-Z]
- Allow space =>
[ ]
or\s
- Allow certain special characters -
_ \ / - . ’ '
=>[_\\\/.’'-]
So, bining all that, the pattern will look like:
ng-pattern="/^[a-zA-Z _\\\/.’'-]+$/"
This matches 1 or more (+
) chars from the allowed set.
To disallow leading/trailing whitespace, use
ng-pattern="/^[a-zA-Z_\\\/.’'-]+(?: +[A-Za-z_\\\/.’'-]+)*$/" ng-trim="false"
See the regex demo
The ng-trim="false"
will prevent from trimming the input sent to the regex engine. The new regex will match:
^
- start of string[a-zA-Z_\\\/.’'-]+
- 1 or more letters or allowed symbols other than space(?: +[A-Za-z_\\\/.’'-]+)*
- zero or more sequences of:+
- (or,\s+
) - 1 or more spaces (whitespaces)[A-Za-z_\\\/.’'-]+
- see above
$
- end of string.
/^([a-z _\/.'-])+$/i
would do. If you want any space character instead of just space, use /(^[a-z_\/.'-]|\s)+$/i
.
本文标签: javascriptngpattern to allow alpha and certain special charactersStack Overflow
版权声明:本文标题:javascript - ng-pattern to allow alpha and certain special characters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741647517a2390273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论