Angularjs 經過directive實現驗證兩次輸入是否一致的功能

實現效果:app

 1> 當輸入確認密碼時驗證:  ide

2> 當輸入密碼時驗證:ui

  

實現步驟:spa

1.頁面代碼:  code

<input class="form-control" type="password" id="password" name="password"
                                                   ng-model="user.password"
                                                   match-check="confrimPassword|confrimPassword">

input class="form-control" type="password" id="confrimPassword"
                                                   name="confrimPassword"
                                                   ng-model="user.confrimPassword"
                                                   match-check="password|confrimPassword" onpaste="return false">
                                            <span class=" error"
                                                  ng-show="!form.password.$pristine && !form.confrimPassword.$pristine && form.confrimPassword.$error.match">This does not match the 'Password' entered above.  Please re-enter your password.</span>
View Code

 

2.自定義驗證指令orm

"use strict";

/**
 * Created by jerry_yang on 2015/11/27.
 * user defined match check directive
 */
angular.module('bet.match', [])
    .directive('matchCheck', matchCheck);

function matchCheck() {
    return {
        require: 'ngModel',
        link: function (scope, elem, attrs, ctrl) {
            var matchCheck = attrs.matchCheck;
            var param = matchCheck.split('|');
            var inputCtr = '#' + param[0];
            elem.bind(inputCtr).on('keyup', function () {
                scope.$apply(function () {
                    var v = elem.val() === $(inputCtr).val();
                    scope.form[param[1]].$setValidity('match', v);
                });
            });
        }
    }
}
View Code
相關文章
相關標籤/搜索