Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 616695e

Browse files
committed
docs: add docs for ngPattern, ngMinlength, ngMaxlength
Closes #9991
1 parent 9ac4e5a commit 616695e

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

src/ng/directive/validators.js

+181
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,74 @@ 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+
input.sendKeys('aaa');
80+
expect(model.getText()).not.toContain('aaa');
81+
82+
input.clear().then(function() {
83+
input.sendKeys('123');
84+
expect(model.getText()).toContain('123');
85+
});
86+
});
87+
* </file>
88+
* </example>
89+
*/
2390
var patternDirective = function() {
2491
return {
2592
restrict: 'A',
@@ -51,7 +118,65 @@ var patternDirective = function() {
51118
};
52119
};
53120

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

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

0 commit comments

Comments
 (0)