Improve this Doc View Source ngPattern
- directive in module ng
ngPattern adds the pattern validator
to ngModel
. It is most often used for text-based input
controls, but can also be applied to custom text-based controls.
The validator sets the pattern
error key if the ngModel.$viewValue
does not match a RegExp which is obtained by evaluating the Angular expression given in the ngPattern
attribute value:
- If the expression evaluates to a RegExp object, then this is used directly.
- If the expression evaluates to a string, then it will be converted to a RegExp after wrapping it in
^
and$
characters. For instance,"abc"
will be converted tonew RegExp('^abc$')
.
Note: Avoid using the
g
flag on the RegExp, as it will cause each successive search to start at the index of the last search's match, thus not taking the whole input value into account. Note: This directive is also added when the plain
pattern
attribute is used, with two differences: -
ngPattern
does not set thepattern
attribute and therefore HTML5 constraint validation is not available. - The
ngPattern
attribute must be an expression, while thepattern
value must be interpolated.
Directive Info
- This directive executes at priority level 0.
Usage
- as attribute:
<ANY> ... </ANY>
© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.4.14/docs/api/ng/directive/ngPattern