ng-pattern allow only letters, numbers, dot, underscore and dash

el.severo picture el.severo · Aug 24, 2016 · Viewed 26.6k times · Source

How'd allow for an input using ng-pattern only: letters, numbers, dot, underscore and dash ( . _ -)?

Already tried the following ones

UPDATE:

$scope.validationPattern = '/^[a-zA-Z\d\-\_]+$/';

<input ng-model="model" type="text" ng-pattern="{{validationPattern}}" />

Answer

Wiktor Stribiżew picture Wiktor Stribiżew · Aug 24, 2016

Judging by your requirements, you need to add a dot to the pattern:

$scope.regex = '^[a-zA-Z0-9._-]+$';

and add ng-trim="false" to the <input> tag so as to disallow leading/trailing spaces.

See this plunkr