form validation in angular

Source: Internet
Author: User

form validation in angular is powerful,

A total of 5 verification information, $valid, $invalid, $pristine, $dirty, $error.

$valid-----When validation passes, true, false when not passed

$invalid----When the validation does not pass, true when passed.

True if the $pristine---is the initial value, false once the change has been made

$dirty---The value has been changed is true, the other is False

$error---Contains all the validation information, such as the following example, $error = {Required:false,minlength:false,pattern:false}. Which validation does not pass, its value is true.

The above five values can be obtained by using the name of the form, as follows:

<form novalidate name= "MyForm" >
<input type= "text" name= "MyText" ng-model= "text" Required ng-minlength= "5" ng-pattern= "/^[a-za-z]+$/" >
<div>{{Myform.mytext. $valid}}</div>
<div>{{Myform.mytext. $invalid}}</div>
<div>{{Myform.mytext. $pristine}}</div>
<div>{{Myform.mytext. $dirty}}</div>
<div>{{Myform.mytext. $error}}</div>
</form>

You can do some form validation with Ng-repeat filter $error, and so on

<script>var m1 = Angular.module (' myApp ', []);    M1.controller (' Aaa ', [' $scope ', function ($scope) {$scope. Text = ' Hello ';           $scope. Regtext = {regT: ' Default ', Reglist: [{name: ' Default ', text: ' Please enter the username '}, {name: ' Required ', text: ' cannot be empty '}, {name: ' pattern ', text: ' Only for Letters '}, {name: ' Pass ', text: ' √            '},], change:function (err) {Console.log (err);                    for (Var attr in err) {if (err[attr]==true) {$scope. Regtext.regt = attr;                Return        }} $scope. regtext.regt = ' pass '; }    };}]); </script>

  

form validation in angular

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.