Although I am not a front-end programmer, I understand how important it is to do verification on the front-end.
Because this way the backend can take a breather, and compared to the backend, the frontend can actually improve the user's happiness.
AngularJS provides a very convenient form validation function, which is recorded here.
First start with the following code
<script><br>
angular.module('myApp',[])<br>
.controller('validationController', ['$scope',function($scope) {<br>
$scope.user = 'Kavlez';<br>
$scope.email = <br>'sweet_dreams@aliyun.com'<a href="mailto:'sweet_dreams@aliyun.com'">;</a>
}]);<br>
</script>
Some validation options for input tags, usually used in conjunction with HTML5 tags.
Required
Length
Use the command ng-minlength/ng-maxlength
Specific format
For example, email, URL, number, just set type to the corresponding type, for example:
Pattern matching
Use directive ng-pattern, for example:
Form properties that make it easier to operate on the form
pristine/dirty
Indicates whether it has been modified, such as
Accessed in formName.fieldName.$pristine mode, input must have ng-model declaration.
valid/invalid
indicates whether the verification is passed.
$error
Form verification information, corresponding information will be returned when verification fails.
AngularJS provides corresponding css class for form status
Copy code The code is as follows:
.ng-pristine
.ng-dirty
.ng-valid
.ng-invalid
For example, make verification pass green and failure red:
input.ng-valid {
Color: green;
}
input.ng-invalid {
Color: green;
}
In the example given, only one email verification is written. If you add several fields, several different prompts, and several events, the code will become messy.
This is actually not recommended, we have a better way.
Just use angular-messages.js
First of all, don’t forget these two steps
angular.module('myApp', ['ngMessages'])
Okay, first replace those duplicates with ng-messages and ng-message. The above example becomes:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
2024-10-22 09:46:29
2024-10-13 13:53:41
2024-10-12 12:15:51
2024-10-11 22:47:31
2024-10-11 19:36:51
2024-10-11 15:50:41
2024-10-11 15:07:41
2024-10-11 14:21:21
2024-10-11 12:59:11
2024-10-11 12:17:31