AngularJS密码确认NOMATCH工作,但形式是$无效? [英] AngularJS password confirmation noMatch working but form is $invalid?
问题描述
我用这code键使密码输入和确认密码输入正确匹配
I am using this code to make password input and confirm password input to match correctly
使用Javascript:
Javascript:
var app = angular.module('app', []);
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
ctrl.$setValidity('noMatch', !noMatch)
});
scope.$watch("reference", function(value) {;
ctrl.$setValidity('noMatch', value === ctrl.$viewValue);
});
}
}
});
app.controller('homeCtrl', function($scope) {
$scope.submit= function (form) {
if(form.$valid)
alert('in');
else
alert('out');
}
});
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="homeCtrl">
<p>Password:{{formData.password}}</p>
<form name="signupForm" ng-submit="submit(signupForm)">
<div class="fieldset" ng-class="{'has-error':formData.password.$invalid && !formData.password.$pristine}">
<label>Password</label>
<input type="password" id="password" name="password" ng-model="formData.password" ng-minlength="8" ng-maxlength="20" ng-pattern="/(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])/" placeholder="password" required />
<p ng-show="signupForm.password.$error.required" class="error">*</p>
<p ng-show="signupForm.password.$error.minlength" class="error">
Passwords must be between 8 and 20 characters.</p>
<p ng-show="signupForm.password.$error.pattern" class="error">
Must contain one lower & uppercase letter, and one non-alpha character (a number or a symbol.)</p>
</div>
<div class="fieldset" ng-class="{'has-error':formData.password_c.$invalid && !formData.password_c.$pristine}">
<label for="password_c">Confirm Password</label>
<input type="password" id="password_c" name="password_c" ng-model="formData.password_c" placeholder="confirm password" valid-password-c="formData.password" required />
<p ng-show="signupForm.password_c.$error.noMatch" class="error">Passwords do not match.</span>
<p ng-show="signupForm.password_c.$error.required" class="error">*</p>
</div>
</form>
</div>
</div>
code工作正常,但是当我通过表单NG-提交功能,它总是说,在确认密码字段无效。我检查这种形式在控制台只能确认密码字段$有效=虚假和无效$ = TRUE。需要帮助的什么是错在这里。
Code is working fine but when I pass form to ng-submit function it always says invalid at confirm password field . I check this form in console only confirm password field has $valid = false and $invalid = true . Need help what is wrong here.
下面是控制台
推荐答案
我不明白为什么你用 $解析器
代替的 $验证
?解析器用于格式化不验证输入字段!这里是我的解决方案:
I don't see why you use $parsers
instead of $validators
? Parsers are used for formatting not for validating an input field! Here's my solution:
/**
* Validator for matching two inputs.
* The given attribute is the value to match the own modelValue.
* @example
* <input name="passwordConfirm" type="password" ng-model="passwordConf" validate-match="newPassword">
*/
app.directive('validateMatch', function () {
return {
require: 'ngModel',
scope: {
validateMatch: '='
},
link: function(scope, element, attrs, ngModel) {
scope.$watch('validateMatch', function() {
ngModel.$validate(); // validate again when match value changes
});
ngModel.$validators.match = function(modelValue) {
if (!modelValue || !scope.validateMatch || ngModel.$error.minlength) {
return true;
}
return modelValue === scope.validateMatch;
};
}
};
});
的 $误差
属性被命名为匹配
不是 NOMATCH
因为那将是双重否定。
The $error
attribute is named match
not noMatch
because that would be double negative.
这篇关于AngularJS密码确认NOMATCH工作,但形式是$无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!