Angular Form Verification instance details, angular form instance

Source: Internet
Author: User

Angular Form Verification instance details, angular form instance

Form Verification

When I went, I felt like I was a very stupid person. It was a waste of time because I had misspelled a word or something, we need to treat this problem correctly. Well, let's get it right. angular also has form verification such as minlength, maxlength, and required, and also supports h5 verification and h5 verification, it's type, type = 'e-mail ', number, url, and so on. Now we need to use angular for verification. we can define the style. That's good. How can we verify it? Good code.

<! DOCTYPE html> 

In this way, you can declare a few points,

First is to introduce angular-messages.js,

Second, the message and messages must be clear,

Third, myForm. name. $ error: This myForm is the name value of form, and name is the name value of input to be verified.

Fourth, this can also be customized for verification.

The fifth sentence has not been mentioned before. You can check out what else to pay attention.

Let's talk about how to implement custom verification. Go to the code.

<input type="text" placeholder="Desired username" name="username" ng-model="signup.username" ng-minlength=3 ng-maxlength=20 ensure-unique="username" required />

You can see that this ensure-unique is a custom verification, that is, it needs to be unique. This is the way it is written in html, and the custom js Code is written by itself, here is the corresponding code, which is written using commands.

angular.module('myApp', []).directive('ensureUnique', ['$http', function($http) {return {require: 'ngModel',link: function(scope, ele, attrs, c) {scope.$watch(attrs.ngModel, function() {$http({method: 'POST',url: '/api/check/' + attrs.ensureUnique,data: {'field': attrs.ensureUnique}}).success(function(data, status, headers, cfg) {c.$setValidity('unique', data.isUnique);}).error(function(data, status, headers, cfg) {c.$setValidity('unique', false);});});}};}]);

I can't see it. html or js completes the custom verification in two steps, that is, the custom verification is a bit low. I am posting these two pieces of code to tell you how to write custom verification code. Haha

Well, there is also the Form Verification attribute of anglar. Let's go to the table and see it at a Glance. This is all available on cainiao. You can search for it.

Then, how can this be used? Okay, go to the code.

<! DOCTYPE html> 

The usage of this statement is summarized as follows,

1. As long as an angular. js is used, no other js needs to be introduced, but the disadvantage is what you know, you cannot verify more conditions. Hey hey, this depends on your needs.

2. that is, she uses ng-show to display the myForm. user. $ error. how does required come from? It's the same as above. It's all name values. This is the case. Do you think required is required, corresponding to this $ dirty so .... myForm. user. $ dirty. Haha, the meanings of these representatives are all in the table. I feel that this verification condition is valid. Let's look at your needs.

3. there is another problem with this usage. For example, you need to verify a required first. If there is no content in the input box at the beginning, his verification prompt will not appear, so you need to first set a default value for js. I don't think this effect is good. So you can see $ scope. user = 'John Doe 'on the page. js assigned him a value first.

4. Another problem is that you need to assign values first, and then you have to create a controller and define a controller. In conclusion, I still think the first method is better.

5. There is another problem. Remember to bind ng-model to each Input. Otherwise, how can we monitor it. My opinion,

There are also the two types that can be combined, but you can't view the code. Okay, go to the code.

<! DOCTYPE html> 

This is the case.

The above is a detailed explanation of Angular form verification examples introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.