密码匹配 angularjs 验证
Password match angularjs validation
正在尝试使用 angular js 中的自定义指令匹配密码。尽管我已经看过几个 google 教程,但我无法让它工作。我创建了一个 Plunker,显示它在 plunker.
时不工作
HTML:
<div class="form-group" ng-class="{ 'has-error': form.password.$invalid && !form.username.$pristine }">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" ng-model="user.password" required ng-minlength="6" ng-maxlength="12"/>
</div>
<div class="form-group" ng-class="{ 'has-error': form.confirm-password.$invalid && !form.confirm-password.$pristine }">
<label for="confirm-password">Confirm Password</label>
<input type="password" name="confirm-password" id="confirm-password" class="form-control" ng-model="user.confirmpwd" required equal="{{user.password}}"/>
<span ng-show="user.confirmpwd.$error.equal" class="help-block">Password does not match above</span>
</div>
JAVASCRIPT:
app.directive('equal', [
function() {
var link = function($scope, $element, $attrs, ctrl) {
var validate = function(viewValue) {
var comparisonModel = $attrs.equal;
console.log(viewValue + ':' + comparisonModel);
if(!viewValue || !comparisonModel){
// It's valid because we have nothing to compare against
ctrl.$setValidity('equal', true);
}
// It's valid if model is lower than the model we're comparing against
ctrl.$setValidity('equal', viewValue === comparisonModel );
return viewValue;
};
ctrl.$parsers.unshift(validate);
ctrl.$formatters.push(validate);
$attrs.$observe('equal', function(comparisonModel){
return validate(ctrl.$viewValue);
});
};
return {
require: 'ngModel',
link: link
};
}]);
问题似乎与自定义指令有关,它似乎没有正确绑定到 ngModel 项目?我觉得我一定缺少一些简单的东西,我才刚刚开始学习AngularJS。
密码字段绑定应该有效,但您正在验证密码字段的长度至少应为 6 个字符,这意味着只有在您输入 6 个或更多字符后,它才会绑定到模型。在那之前它将是 undefined
,这就是你在我假设的 console.log
语句中得到的。
但是还有其他问题。不会显示错误消息,因为您的字段名称是 confirm-password
。您应该将其命名为 confirmPassword
或不带破折号的名称。该名称被 Angular 用作对象 属性 并且 JavaScript 对象键不能包含破折号。
因此,您表单中的密码确认部分应如下所示:
<div class="form-group" ng-class="{ 'has-error': form.confirmPassword.$invalid && !form.confirmPassword.$pristine }">
<label for="confirm-password">Confirm Password</label>
<input type="password" name="confirmPassword" id="confirm-password" class="form-control" ng-model="user.confirmpwd" required equal="{{user.password}}"/>
<span ng-show="form.confirmPassword.$error.equal" class="help-block">Password does not match above</span>
</div>
正在尝试使用 angular js 中的自定义指令匹配密码。尽管我已经看过几个 google 教程,但我无法让它工作。我创建了一个 Plunker,显示它在 plunker.
时不工作HTML:
<div class="form-group" ng-class="{ 'has-error': form.password.$invalid && !form.username.$pristine }">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" ng-model="user.password" required ng-minlength="6" ng-maxlength="12"/>
</div>
<div class="form-group" ng-class="{ 'has-error': form.confirm-password.$invalid && !form.confirm-password.$pristine }">
<label for="confirm-password">Confirm Password</label>
<input type="password" name="confirm-password" id="confirm-password" class="form-control" ng-model="user.confirmpwd" required equal="{{user.password}}"/>
<span ng-show="user.confirmpwd.$error.equal" class="help-block">Password does not match above</span>
</div>
JAVASCRIPT:
app.directive('equal', [
function() {
var link = function($scope, $element, $attrs, ctrl) {
var validate = function(viewValue) {
var comparisonModel = $attrs.equal;
console.log(viewValue + ':' + comparisonModel);
if(!viewValue || !comparisonModel){
// It's valid because we have nothing to compare against
ctrl.$setValidity('equal', true);
}
// It's valid if model is lower than the model we're comparing against
ctrl.$setValidity('equal', viewValue === comparisonModel );
return viewValue;
};
ctrl.$parsers.unshift(validate);
ctrl.$formatters.push(validate);
$attrs.$observe('equal', function(comparisonModel){
return validate(ctrl.$viewValue);
});
};
return {
require: 'ngModel',
link: link
};
}]);
问题似乎与自定义指令有关,它似乎没有正确绑定到 ngModel 项目?我觉得我一定缺少一些简单的东西,我才刚刚开始学习AngularJS。
密码字段绑定应该有效,但您正在验证密码字段的长度至少应为 6 个字符,这意味着只有在您输入 6 个或更多字符后,它才会绑定到模型。在那之前它将是 undefined
,这就是你在我假设的 console.log
语句中得到的。
但是还有其他问题。不会显示错误消息,因为您的字段名称是 confirm-password
。您应该将其命名为 confirmPassword
或不带破折号的名称。该名称被 Angular 用作对象 属性 并且 JavaScript 对象键不能包含破折号。
因此,您表单中的密码确认部分应如下所示:
<div class="form-group" ng-class="{ 'has-error': form.confirmPassword.$invalid && !form.confirmPassword.$pristine }">
<label for="confirm-password">Confirm Password</label>
<input type="password" name="confirmPassword" id="confirm-password" class="form-control" ng-model="user.confirmpwd" required equal="{{user.password}}"/>
<span ng-show="form.confirmPassword.$error.equal" class="help-block">Password does not match above</span>
</div>