Javascript – ng-pattern to allow spaces between words

angularjsjavascriptregex

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>

Best Answer

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.