Skip to content

Commit 974e7ea

Browse files
committed
docs: add docs for ngPattern, ngMinlength, ngMaxlength
Closes angular#9991
1 parent d9ec995 commit 974e7ea

File tree

1 file changed

+181
-1
lines changed

1 file changed

+181
-1
lines changed

src/ng/directive/validators.js

+181-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,76 @@ var requiredDirective = function() {
1919
};
2020
};
2121

22+
/**
23+
* @ngdoc directive
24+
* @name ngPattern
25+
*
26+
* @description
27+
*
28+
* ngPattern adds the pattern {@link ngModel.NgModelController#$validators `validator`} to {@link ngModel `ngModel`}.
29+
* It is most often used for text-based [@link input `input`} controls, but can also be applied to custom text-based controls.
30+
*
31+
* The validator sets the `pattern` error key if the {@link ngModel.NgModelController#$viewValue `ngModel.$viewValue`}
32+
* does not match a RegExp which is obtained by evaluating the Angular expression given in the
33+
* `ngPattern` attribute value:
34+
* * If the expression evaluates to a RegExp object, then this is used directly.
35+
* * If the expression evaluates to a string, then it will be converted to a RegExp after wrapping it
36+
* in `^` and `$` characters. For instance, `"abc"` will be converted to `new RegExp('^abc$')`.
37+
*
38+
* <div class="alert alert-info">
39+
* **Note:** Avoid using the `g` flag on the RegExp, as it will cause each successive search to
40+
* start at the index of the last search's match, thus not taking the whole input value into
41+
* account.
42+
* </div>
43+
*
44+
* <div class="alert alert-info">
45+
* **Note:** This directive is also added when the plain `pattern` attribute is used, with two
46+
* differences:
47+
* 1. `ngPattern` does not set the `pattern` attribute and therefore not HTML5 constraint validation
48+
* is available.
49+
* 2. The `ngPattern` attribute must be an expression, while the `pattern` value must be interpolated
50+
* </div>
51+
*
52+
* @example
53+
* <example name="ngPatternDirective" module="ngPatternExample">
54+
* <file name="index.html">
55+
* <script>
56+
* angular.module('ngPatternExample', [])
57+
* .controller('ExampleController', ['$scope', function($scope) {
58+
* $scope.regex = '\\d+';
59+
* }]);
60+
* </script>
61+
* <div ng-controller="ExampleController">
62+
* <form name="form">
63+
* <label for="regex">Set a pattern (regex string): </label>
64+
* <input type="text" ng-model="regex" id="regex" />
65+
* <br>
66+
* <label for="input">This input is restricted by the current pattern: </label>
67+
* <input type="text" ng-model="model" id="input" name="input" ng-pattern="regex" /><br>
68+
* <hr>
69+
* input valid? = <code>{{form.input.$valid}}</code><br>
70+
* model = <code>{{model}}</code>
71+
* </form>
72+
* </div>
73+
* </file>
74+
* <file name="protractor.js" type="protractor">
75+
var model = element(by.binding('model'));
76+
var input = element(by.id('input'));
2277
78+
it('should validate the input with the default pattern', function() {
79+
expect(regex.getText()).toContain('\d+');
80+
81+
input.sendKeys('aaa');
82+
expect(model.getText()).not.toContain('aaa');
83+
84+
input.clear().then(function() {
85+
input.sendKeys('123');
86+
expect(model.getText()).toContain('123');
87+
});
88+
});
89+
* </file>
90+
* </example>
91+
*/
2392
var patternDirective = function() {
2493
return {
2594
restrict: 'A',
@@ -51,7 +120,62 @@ var patternDirective = function() {
51120
};
52121
};
53122

54-
123+
/**
124+
* @ngdoc directive
125+
* @name ngMaxlength
126+
*
127+
* @description
128+
*
129+
* ngMaxlength adds the maxlength {@link ngModel.NgModelController#$validators `validator`} to {@link ngModel `ngModel`}.
130+
* It is most often used for text-based [@link input `input`} controls, but can also be applied to custom text-based controls.
131+
*
132+
* The validator sets the `maxlength` error key if the {@link ngModel.NgModelController#$viewValue `ngModel.$viewValue`}
133+
* is longer than the integer obtained by evaluating the Angular expression given in the
134+
* `ngMaxlength` attribute value.
135+
*
136+
* <div class="alert alert-info">
137+
* **Note:** This directive is also added when the plain `maxlength` attribute is used, with two
138+
* differences:
139+
* 1. `ngMaxlength` does not set the `maxlength` attribute and therefore not HTML5 constraint validation
140+
* is available.
141+
* 2. The `ngMaxlength` attribute must be an expression, while the `maxlength` value must be interpolated
142+
* </div>
143+
*
144+
* @example
145+
* <example name="ngMaxlengthDirective" module="ngMaxlengthExample">
146+
* <file name="index.html">
147+
* <script>
148+
* angular.module('ngMaxlengthExample', [])
149+
* .controller('ExampleController', ['$scope', function($scope) {
150+
* $scope.maxlength = 5;
151+
* }]);
152+
* </script>
153+
* <div ng-controller="ExampleController">
154+
* <form name="form">
155+
* <label for="maxlength">Set a maxlength: </label>
156+
* <input type="number" ng-model="maxlength" id="maxlength" />
157+
* <br>
158+
* <label for="input">This input is restricted by the current maxlength: </label>
159+
* <input type="text" ng-model="model" id="input" name="input" ng-maxlength="maxlength" /><br>
160+
* <hr>
161+
* input valid? = <code>{{form.input.$valid}}</code><br>
162+
* model = <code>{{model}}</code>
163+
* </form>
164+
* </div>
165+
* </file>
166+
* <file name="protractor.js" type="protractor">
167+
* var model = element(by.binding('model'));
168+
*
169+
* it('should validate the input with the default maxlength', function() {
170+
* element(by.id('input')).sendKeys('abcdef');
171+
* expect(model.getText()).not.toContain('abcdef');
172+
*
173+
* element(by.id('input')).sendKeys('abcde');
174+
* expect(model.getText()).toContain('abcde');
175+
* });
176+
* </file>
177+
* </example>
178+
*/
55179
var maxlengthDirective = function() {
56180
return {
57181
restrict: 'A',
@@ -72,6 +196,62 @@ var maxlengthDirective = function() {
72196
};
73197
};
74198

199+
/**
200+
* @ngdoc directive
201+
* @name ngMinlength
202+
*
203+
* @description
204+
*
205+
* ngMinlength adds the minlength {@link ngModel.NgModelController#$validators `validator`} to {@link ngModel `ngModel`}.
206+
* It is most often used for text-based [@link input `input`} controls, but can also be applied to custom text-based controls.
207+
*
208+
* The validator sets the `minlength` error key if the {@link ngModel.NgModelController#$viewValue `ngModel.$viewValue`}
209+
* is shorter than the integer obtained by evaluating the Angular expression given in the
210+
* `ngMinlength` attribute value.
211+
*
212+
* <div class="alert alert-info">
213+
* **Note:** This directive is also added when the plain `minlength` attribute is used, with two
214+
* differences:
215+
* 1. `ngMinlength` does not set the `minlength` attribute and therefore not HTML5 constraint validation
216+
* is available.
217+
* 2. The `ngMinlength` value must be an expression, while the `minlength` value must be interpolated
218+
* </div>
219+
*
220+
* @example
221+
* <example name="ngMinlengthDirective" module="ngMinlengthExample">
222+
* <file name="index.html">
223+
* <script>
224+
* angular.module('ngMinlengthExample', [])
225+
* .controller('ExampleController', ['$scope', function($scope) {
226+
* $scope.minlength = 3;
227+
* }]);
228+
* </script>
229+
* <div ng-controller="ExampleController">
230+
* <form name="form">
231+
* <label for="minlength">Set a minlength: </label>
232+
* <input type="number" ng-model="minlength" id="minlength" />
233+
* <br>
234+
* <label for="input">This input is restricted by the current minlength: </label>
235+
* <input type="text" ng-model="model" id="input" name="input" ng-minlength="minlength" /><br>
236+
* <hr>
237+
* input valid? = <code>{{form.input.$valid}}</code><br>
238+
* model = <code>{{model}}</code>
239+
* </form>
240+
* </div>
241+
* </file>
242+
* <file name="protractor.js" type="protractor">
243+
* var model = element(by.binding('model'));
244+
*
245+
* it('should validate the input with the default minlength', function() {
246+
* element(by.id('input')).sendKeys('ab');
247+
* expect(model.getText()).not.toContain('ab');
248+
*
249+
* element(by.id('input')).sendKeys('abc');
250+
* expect(model.getText()).toContain('abc');
251+
* });
252+
* </file>
253+
* </example>
254+
*/
75255
var minlengthDirective = function() {
76256
return {
77257
restrict: 'A',

0 commit comments

Comments
 (0)