Search code examples
angularjsregexng-pattern

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


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}}" />

Solution

  • 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