繁体   English   中英

验证时使另一个字段无效

[英]Make another field invalid while validating

我正在处理一个标准的“更改您的密码”表格,其中有两个字段:password1和password2。 这两个字段仅用于验证用户输入了正确的密码,并且它们需要包含相同的文本。

我添加了一条指令来验证密码,但是现在我想要的是,如果两个字段彼此之间不相等,则使两个字段都变得无效,而不仅仅是我输入的字段无效。我能这样做吗?

我尝试在两个ngmodels上调用$ setValidity,但是我没有找到从一个ctrl。$ parsers.unshift或指令链接函数调用$ setValidity或我当前未验证的其他字段的方法。 我真的迷路了。

非常感谢!

我的指令是:

myApp.directive('validatepassword', function () {
    return {
        require: 'ngModel',
        restrict: 'A', // only activate on element attribute
        link: function (scope, elm, attrs, ngModel) {
            ngModel.$parsers.unshift(function (viewValue) {
                var valPasswordValue = attrs.validatepassword;
                var otherPassword = $('#' + valPasswordValue)[0].value;
                var valido = scope.validatePassword(viewValue, otherPassword);
                ngModel.$setValidity('password', valido);
                return viewValue;
            });
        }
    };
});

我在代码中以这种方式使用:

        <div class="control-group">
            <label class="control-label" for="inputPassword1">Password</label>
            <div class="controls">
                <input id="inputPassword1" name="inputPassword1" type="password" ng-model="context.Password" required validatepassword="inputPassword2"/>
                <span class="alert-danger invalid-form" ng-show="!addEditForm.inputPassword1.$valid">(*)</span>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputPassword2">Repeat Password</label>
            <div class="controls">
                <input id="inputPassword2" name="inputPassword2" type="password" ng-model="context.Password2" required validatepassword="inputPassword1"/>
                <span class="alert-danger invalid-form" ng-show="!addEditForm.inputPassword2.$valid">(*)</span>
            </div>
        </div>

关于如何立即更改两个字段的任何想法?

非常感谢!

要从另一个字段的一个字段触发验证方法,我必须手动设置另一个字段的值。 您可以在ng-change内部执行此操作:

ng-change="addEditForm.inputPassword2.$setViewValue(addEditForm.inputPassword2.$viewValue)"

这样做时,它应该在两个密码字段中触发验证。

您也可以像这样访问指令内部的字段:

scope.addEditForm.inputPassword1

因此,您可以摆脱指令内部的jQuery访问。

这是在两个字段上用于密码验证的HTML部分:

<div class="control-group">
    <label class="control-label" for="inputPassword1">Password</label>

    <div class="controls">
        <input id="inputPassword1" name="inputPassword1" type="password" ng-model="context.Password" required
               validatepassword="inputPassword2" ng-change="addEditForm.inputPassword2.$setViewValue(addEditForm.inputPassword2.$viewValue)"/>
        <span class="alert-danger invalid-form" ng-show="!addEditForm.inputPassword1.$valid">(*)</span>
    </div>
</div>
<div class="control-group">
    <label class="control-label" for="inputPassword2">Repeat Password</label>

    <div class="controls">
        <input id="inputPassword2" name="inputPassword2" type="password" ng-model="context.Password2" required
               validatepassword="inputPassword1" ng-change="addEditForm.inputPassword1.$setViewValue(addEditForm.inputPassword1.$viewValue)"/>
        <span class="alert-danger invalid-form" ng-show="!addEditForm.inputPassword2.$valid">(*)</span>
    </div>
</div>

Angular-UI有一个内置的验证器,您可以将其用于各种目的,这里有一个确切的密码示例,您可以检查并确认密码: https : //github.com/angular-ui/ui-utils/blob/ master / modules / validate / demo / index.html#L29

请检查此小提琴http://jsfiddle.net/vigneshvdm/Dnt7w/5/

你可以做这样的事情

var password=$("#telephone").val();
var reenteredpassword=$("#mobile").val();

if(password==reenteredpassword)
{
$("#required").html("Passwords Match");
}
else
{
$("#required").html("Passwords do not Match");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM