我正在尝试使用相同的确认密码功能,并遵循@sylvester建议的方法,但是我表单上的确认密码字段无法显示错误或对此问题的验证。请在下面找到表单字段,并在下面找到指令。
预先感谢
app.directive('validPasswordC', function() {
return {
require: 'ngModel',
scope: {
reference: '=validPasswordC'
},
link: function(scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function(viewValue, $scope) {
// var noMatch = viewValue != scope.reference
var noMatch = viewValue != scope.reference.$viewValue;
ctrl.$setValidity('noMatch', !noMatch);
return (noMatch)?noMatch:!noMatch;
});
scope.$watch("reference", function(value) {;
ctrl.$setValidity('noMatch', value === ctrl.$viewValue);
});
}
}
});
<div class="form-group" ng-class="{'has-error':registerctrl.form.psw.$dirty && registerctrl.form.psw.$error.required}">
<label for="psw"><b>Password:</b></label>
<input type="password" class="form-control" placeholder="Enter Password" ng-model="registerctrl.psw"
name="psw" ng-minlength="8" ng-maxlength="20" ng-pattern="/(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])/"
required>
<span ng-show="registerctrl.form.psw.$dirty && registerctrl.form.psw.$error.required" class="help-block">Password is required</span>
<p ng-show="registerctrl.form.psw.$error.minlength" class="error">
Passwords must be between 8 and 20 characters.</p>
<p ng-show="registerctrl.form.psw.$error.pattern" class="error">
Must contain one lower & uppercase letter, and one non-alpha character (a number or a symbol.)</p>
</div>
<div class="form-group" ng-class="{'has-error':registerctrl.form.psw-repeat.$dirty && registerctrl.form.psw-repeat.$error.required}">
<label for="psw-repeat"><b>Repeat Password:</b></label>
<input type="password" class="form-control" placeholder="Repeat Password" ng-model="registerctrl.pswrepeat" name="psw-repeat" valid-password-c="registerctrl.form.psw"
required>
<p ng-show="registerctrl.form.psw-repeat.$error.noMatch" class="error">Passwords do not match.</p>
</div>