Angular form Validation examples _angularjs

Source: Internet
Author: User
Tags validation examples

Form validation

I go, I feel that I am actually a very stupid person, always because misspelled a word or how to waste me a long time, so really do not do not, to correct the problem, well, to get to the point, angular also has a form to verify Minlength,maxlength, Required AH these things, there are also support h5 of those verification, H5 of those verification, is type, type= ' email ', number,url ah these, and now to use the angular to verify, you can define the style ha, yes, then how to verify it, Okay, code.

<!
DOCTYPE html>  

That's it, there are a few points to be stated,

The first is to introduce Angular-messages.js,

The second is message and messages must see clearly ah,

The third myform.name $error: This myform is the name value of the form, and name is the name of the input to be validated.

Four is this also can be customized validation ha.

En-Five words Mingmai not yet, the great God can see what else to pay attention to, welcome to point out.

Custom validation What the hell's going on here, code.

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

You see this ensure-unique is a custom verification, that is, the need for a unique chant, this is the writing on the HTML, custom JS code is their own write, here also has a corresponding code, is written with instructions, Grace on the code

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);
})
;};}; }]);

See, Html,js Two steps to complete a custom validation, which is custom validation I feel a little bit low, I'm a little low. Anyway, I'm posting these two pieces of code to tell you how the custom validation is written, haha

Well, there's the Anglar form validation properties ha, anyway, on the table, at a glance ha, this is a rookie online there, you can go search search ha

And then, how does this work, okay, on the code

<! DOCTYPE html> 
<!
DOCTYPE html>  

That's the way it is.

The above is a small set to introduce the angular form verification examples, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.