最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ng-pattern to allow spaces between words - Stack Overflow

programmeradmin0浏览0评论

I am looking for a regular expression that prevents special characters and only allows letters, numbers, dash (-), underscore (_) an space.

This regex works great but it doesn't allow for spaces between words.

For example, if enter "123 456", i get custom error i defined. How i can tweak this reg to allow space between words as well in addition to letters, numbers, dash and underscore.

<input type="text" name="serialNumber" data-ng-model="SerialNumber" ng-pattern="/^[a-zA-Z0-9_-]*$/" maxlength="100"/>
<div data-ng-if="Form.serialNumber.$error.pattern" class="error">Please enter valid serial number</div>

I am looking for a regular expression that prevents special characters and only allows letters, numbers, dash (-), underscore (_) an space.

This regex works great but it doesn't allow for spaces between words.

For example, if enter "123 456", i get custom error i defined. How i can tweak this reg to allow space between words as well in addition to letters, numbers, dash and underscore.

<input type="text" name="serialNumber" data-ng-model="SerialNumber" ng-pattern="/^[a-zA-Z0-9_-]*$/" maxlength="100"/>
<div data-ng-if="Form.serialNumber.$error.pattern" class="error">Please enter valid serial number</div>
Share Improve this question edited Jan 9, 2018 at 5:02 KARTHIKEYAN.A 20.1k9 gold badges135 silver badges149 bronze badges asked Aug 19, 2015 at 6:52 TheKingPinMirzaTheKingPinMirza 8,9226 gold badges53 silver badges83 bronze badges 4
  • did you want to allow spaces or hypen or underscore at the start or at the end? – Avinash Raj Commented Aug 19, 2015 at 6:56
  • DO you want to allow empty value? – vks Commented Aug 19, 2015 at 6:57
  • @ Avinash Raj and @vks yes i would like to allow only hypen or underscore at the start and not space. – TheKingPinMirza Commented Aug 19, 2015 at 7:01
  • @Avinash Raj Only alphanumeric, _, - and space between words. – TheKingPinMirza Commented Aug 19, 2015 at 7:12
Add a comment  | 

6 Answers 6

Reset to default 8

I think I found a very simple and basic solution for my requirement i.e. to allow alphanumeric, _, - and space. Space should be allowed between word and not leading and trailing space.

ng-pattern="/^[a-zA-Z0-9\_\- ]*$/"

Anybody find issue with it , let me know please.

If you want to allow a space in your input, you need to include it into the character class.

According to docs:

ngTrim (optional)
If set to false Angular will not automatically trim the input. This parameter is ignored for input[type=password] controls, which will never trim the input.
(default: true)

So, if your input field is not password field I'd recommend:

ng-pattern="/^[\w -]*$/"

The \w stands for [A-Za-z0-9_] in JS, the underscore does not have to be escaped and the hyphen at the end of the character class does not need escaping either.

^[a-zA-Z0-9_-]+(?:\s+[a-zA-Z0-9_-]+)*$

Construct your regex this way to capture words and not spaces at start or end.

You may try the below lookaround based regex.

^(?!\s)(?=$|.*[a-zA-Z0-9_-]$)[a-zA-Z0-9_\s-]*$

Use this pattern to solve the issue

ng-pattern="/^(\D)+$/"

pls ng-pattern="/^[\w -]*$/"

发布评论

评论列表(0)

  1. 暂无评论