el.severo
el.severo

Reputation: 2287

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

Upvotes: 5

Views: 31225

Answers (3)

Gandalf the White
Gandalf the White

Reputation: 2465

/^[a-zA-Z0-9 ._-]+$/

This regex seems appropriate enough for you if it don't directly fit in ng-pattern make necessary adjustments, like.

in controller add

$scope.pattern = /^[a-zA-Z0-9 ._-]+$/;

now in html use ng-pattern="pattern"

Tested using this : http://www.regextester.com/

Accepts numbers, alphabets and three symbols(.-_)

Upvotes: 2

jayanthCoder
jayanthCoder

Reputation: 873

[a-zA-Z0-9][A-Za-z0-9._-]*

this one works

Upvotes: 4

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627380

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

Upvotes: 13

Related Questions