Angularjs Methods for verifying duplicate passwords (two) _angularjs

Source: Internet
Author: User

This article gives you two ways to share angularjs validation of duplicate passwords. Details of the specific method are as follows:

First type:

<label for= "password" > Password </label> <input id= "password" name= "password" type= "password" ng-model= "
User.password "required>
<label for=" Repassword "> Duplicate password </label>
<input id=" Repassword " Name= "Repassword" type= "password" ng-model= "Repassword" required> <span style= "color:red" ng-show= "
User.password!=repassword "> Two password inconsistency </span>
<input type=" Submit "class=" BTN btn-primary btn-lg "value = "SAVE" ng-disabled= "Submit (UserForm)"/>
/*js*/
app.controller ("main", function ($scope) {
$ Scope.submit=function (Ngformcontroller) {return
ngformcontroller. $invalid;/*valid's Take-back */
};

This is simply to determine whether the value of the two Ng-model is equal, unequal to show the ng-show Command control information, etc. are hidden.

But while this approach is simple, there is a flaw that I think is more serious: this "password inconsistency" does not affect the interior of the Ngformcontroller. That is, even if it is incorrectly coded twice, the final submit button can be clicked, because Ngformcontroller's $invalid does not think two passwords incorrectly are a mistake.

Refer to the ANGULARJS instructions Ng-maxlength and so on, they can cause $invalid detection, so to solve the above problem, I think one of the ways is to create a two password to verify the consistency of the custom instructions.

/* directive creation/app.directive (' Equals ', function () {return{require: ' Ngmodel ', link:function (Scope,elm,attrs,ngmodelctrl) { function Validateequal (myvalue) {var valid = (MyValue = = scope. $eval (attrs.equals)) Ngmodelctrl. $setValidity (' equal ')
, valid); Return valid?
myvalue:undefined;
Ngmodelctrl. $parsers. push (Validateequal);
Ngmodelctrl. $formatters. push (Validateequal);
Scope. $watch (Attrs.equals,function () {Ngmodelctrl. $setViewValue (Ngmodelctrl. $viewValue);})}
}
}); <!--html--> <label for= "password" > Password </label> <input id= "password" name= "password" type= " Password "ng-model=" User.password "required> <label for=" Repassword "> Duplicate password </label> <input id=" Repassword "Name=" Repassword "type=" password "ng-model=" Repassword "<!--note that I'm going to use my custom directive-->equals="
User.password "required> <span style=" color:red "ng-show=" User.password!=repassword "> two times Password inconsistency </span> <input type= "Submit" class= "btn btn-primary btn-lg" value= "SAVE" ng-disabled= "Submit (UserfORM) "/> 

This way, coupled with the first method of judgment, you can perfectly verify the duplicate password.

The above is a small set to introduce the ANGULARJS to verify the method of duplicate password, 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!

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.