aboutsummaryrefslogtreecommitdiffstats
path: root/docs/content/guide/dev_guide.templates.validators.ngdoc
diff options
context:
space:
mode:
Diffstat (limited to 'docs/content/guide/dev_guide.templates.validators.ngdoc')
-rw-r--r--docs/content/guide/dev_guide.templates.validators.ngdoc29
1 files changed, 0 insertions, 29 deletions
diff --git a/docs/content/guide/dev_guide.templates.validators.ngdoc b/docs/content/guide/dev_guide.templates.validators.ngdoc
index 4976b8de..76df92b5 100644
--- a/docs/content/guide/dev_guide.templates.validators.ngdoc
+++ b/docs/content/guide/dev_guide.templates.validators.ngdoc
@@ -3,11 +3,9 @@
@name Developer Guide: Templates: Understanding Angular Validators
@description
-
Angular validators are attributes that test the validity of different types of user input. Angular
provides a set of built-in input validators:
-
* {@link api/angular.validator.phone phone number}
* {@link api/angular.validator.number number}
* {@link api/angular.validator.integer integer}
@@ -18,35 +16,26 @@ provides a set of built-in input validators:
* {@link api/angular.validator.url URLs}
* {@link api/angular.validator.asynchronous asynchronous}
-
You can also create your own custom validators.
-
# Using Angular Validators
-
You can use angular validators in HTML template bindings, and in JavaScript:
-
* Validators in HTML Template Bindings
-
<pre>
<input ng:validator="validator_type:parameters" [...]>
</pre>
-
* Validators in JavaScript
-
<pre>
angular.validator.[validator_type](parameters)
</pre>
-
The following example shows how to use the built-in angular integer validator:
-
<doc:example>
<doc:source>
Change me: <input type="text" name="number" ng:validate="integer" value="123">
@@ -64,19 +53,15 @@ The following example shows how to use the built-in angular integer validator:
</doc:scenario>
</doc:example>
-
# Creating an Angular Validator
-
To create a custom validator, you simply add your validator code as a method onto the
`angular.validator` object and provide input(s) for the validator function. Each input provided is
treated as an argument to the validator function. Any additional inputs should be separated by
commas.
-
The following bit of pseudo-code shows how to set up a custom validator:
-
<pre>
angular.validator('your_validator', function(input [,additional params]) {
[your validation code];
@@ -88,22 +73,17 @@ angular.validator('your_validator', function(input [,additional params]) {
}
</pre>
-
Note that this validator returns "true" when the user's input is incorrect, as in "Yes, it's true,
there was a problem with that input". If you prefer to provide more information when a validator
detects a problem with input, you can specify an error message in the validator that angular will
display when the user hovers over the input widget.
-
To specify an error message, replace "`return true;`" with an error string, for example:
-
return "Must be a value between 1 and 5!";
-
Following is a sample UPS Tracking Number validator:
-
<doc:example>
<doc:source>
<script>
@@ -122,7 +102,6 @@ it('should validate correct UPS tracking number', function() {
not().toMatch(/ng-validation-error/);
});
-
it('should not validate in correct UPS tracking number', function() {
input('trackNo').enter('foo');
expect(element('input[name=trackNo]').attr('class')).
@@ -131,30 +110,22 @@ it('should not validate in correct UPS tracking number', function() {
</doc:scenario>
</doc:example>
-
In this sample validator, we specify a regular expression against which to test the user's input.
Note that when the user's input matches `regexp`, the function returns "false" (""); otherwise it
returns the specified error message ("true").
-
Note: you can also access the current angular scope and DOM element objects in your validator
functions as follows:
-
* `this` === The current angular scope.
* `this.$element` === The DOM element that contains the binding. This allows the filter to
manipulate the DOM in addition to transforming the input.
-
-
## Related Topics
-
* {@link dev_guide.templates Angular Templates}
-
## Related API
-
* {@link api/angular.validator Validator API}