AngularJS implementation Form Verification _ AngularJS-js tutorial

Source: Internet
Author: User
Tags aliyun
Client form verification is one of the coolest functions in AngularJS. AngularJS Form Verification allows you to write an interactive and modern HTML5 form from the very beginning. There are many Form Verification commands in AngularJS. Here we will talk about several of the most popular commands, and then we will discuss how to write custom verification. Although I am not a front-end programmer, I understand how important the front-end verification is.

In this way, the backend can breathe more breath, and the frontend can improve the user's happiness compared with the backend.

AngularJS provides a convenient form verification function, which is recorded here.

Start with the following code:

The Code is as follows:



Script
Angular. module ('myapp', [])
. Controller ('validationcontroller', ['$ scope', function ($ scope ){
$ Scope. user = 'kavlez ';
$ Scope. email = 'sweet _ dreams@aliyun.com ';
}]);
Script

Some verification options of the input tag are usually used with the HTML5 tag.

Required


Length

Use command ng-minlength/ng-maxlength


Specific format
For example, set the email, URL, and number type to the corresponding type, for example:

The Code is as follows:





Pattern Matching

Use the command ng-pattern, for example:

The Code is as follows:



Form attributes can be used to operate the form more easily.

Pristine/dirty
Indicates whether it has been modified, for example

The Code is as follows:



Access using formName. fieldName. $ pristine. The input must have an ng-model declaration.

The Code is as follows:


Valid/invalid

Indicates whether the verification is successful.

The Code is as follows:


$ Error

Form Verification information. If the verification fails, the corresponding information is returned.

AngularJS provides css class for the form status.

The Code is as follows:


. Ng-pristine
. Ng-dirty
. Ng-valid
. Ng-invalid

For example, to make the verification pass green, the failure is Red:

The Code is as follows:


Input. ng-valid {
Color: green;
}
Input. ng-invalid {
Color: green;
}

In the example given, the code is written as much as an email verification. If a few fields are added, several different prompts are added, and several events are added, the code will become messy.

In fact, this is not recommended. We have a better method.
Is to use angular-messages.js

First, do not forget the two steps

The Code is as follows:


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.