Javascript 如何检查密码确认?

Javascript 如何检查密码确认?,javascript,angularjs,validation,ionic-framework,ng-messages,Javascript,Angularjs,Validation,Ionic Framework,Ng Messages,情况: <form name="autentication_form" novalidate="" ng-submit="submit_register()"> <label class="item item-input"> <span class="input-label">Email</span> <input type="text" name="email" ng-model="register

情况:

<form name="autentication_form" novalidate="" ng-submit="submit_register()">

    <label class="item item-input">
        <span class="input-label">Email</span>
        <input type="text" name="email" ng-model="registerData.email" required>

        <div class="form-errors" ng-messages="autentication_form.email.$error" role="alert" ng-if='autentication_form.email.$dirty'>
            <div class="form-error" ng-message="required">You did not enter the email</div>
        </div>
    </label>

    <label class="item item-input">
        <span class="input-label">Password</span>
        <input type="password" name="password" ng-model="registerData.password" required>

        <div class="form-errors" ng-messages="autentication_form.password.$error" role="alert" ng-if='autentication_form.password.$dirty'>
            <div class="form-error" ng-message="required">Please enter the password</div>
        </div>
    </label>

    <label class="item item-input">
        <span class="input-label">Password confirmation</span>
        <input type="password" name="password_confirmation" ng-model="registerData.password_confirmation" required>

        <div class="form-errors" ng-messages="autentication_form.password_confirmation.$error" role="alert" ng-if='autentication_form.password_confirmation.$dirty'>
            <div class="form-error" ng-message="required">Password confirmation required</div>
        </div>
    </label>

</form>
我在angular应用程序中使用指令进行即时验证。除了一件事之外,一切都很正常:我需要验证“密码确认”字段,但不知道怎么做

代码:

<form name="autentication_form" novalidate="" ng-submit="submit_register()">

    <label class="item item-input">
        <span class="input-label">Email</span>
        <input type="text" name="email" ng-model="registerData.email" required>

        <div class="form-errors" ng-messages="autentication_form.email.$error" role="alert" ng-if='autentication_form.email.$dirty'>
            <div class="form-error" ng-message="required">You did not enter the email</div>
        </div>
    </label>

    <label class="item item-input">
        <span class="input-label">Password</span>
        <input type="password" name="password" ng-model="registerData.password" required>

        <div class="form-errors" ng-messages="autentication_form.password.$error" role="alert" ng-if='autentication_form.password.$dirty'>
            <div class="form-error" ng-message="required">Please enter the password</div>
        </div>
    </label>

    <label class="item item-input">
        <span class="input-label">Password confirmation</span>
        <input type="password" name="password_confirmation" ng-model="registerData.password_confirmation" required>

        <div class="form-errors" ng-messages="autentication_form.password_confirmation.$error" role="alert" ng-if='autentication_form.password_confirmation.$dirty'>
            <div class="form-error" ng-message="required">Password confirmation required</div>
        </div>
    </label>

</form>

电子邮件
您没有输入电子邮件
密码
请输入密码
密码确认
需要密码确认
问题:

<form name="autentication_form" novalidate="" ng-submit="submit_register()">

    <label class="item item-input">
        <span class="input-label">Email</span>
        <input type="text" name="email" ng-model="registerData.email" required>

        <div class="form-errors" ng-messages="autentication_form.email.$error" role="alert" ng-if='autentication_form.email.$dirty'>
            <div class="form-error" ng-message="required">You did not enter the email</div>
        </div>
    </label>

    <label class="item item-input">
        <span class="input-label">Password</span>
        <input type="password" name="password" ng-model="registerData.password" required>

        <div class="form-errors" ng-messages="autentication_form.password.$error" role="alert" ng-if='autentication_form.password.$dirty'>
            <div class="form-error" ng-message="required">Please enter the password</div>
        </div>
    </label>

    <label class="item item-input">
        <span class="input-label">Password confirmation</span>
        <input type="password" name="password_confirmation" ng-model="registerData.password_confirmation" required>

        <div class="form-errors" ng-messages="autentication_form.password_confirmation.$error" role="alert" ng-if='autentication_form.password_confirmation.$dirty'>
            <div class="form-error" ng-message="required">Password confirmation required</div>
        </div>
    </label>

</form>

如何使用ng消息检查密码确认是否匹配?

最好的方法是使用指令。这里的主要问题是,密码和密码确认输入都需要监视

这里有一个解决方案可能会有所帮助

angular.module('app',['ngMessages']))
.controller('ctrl',函数($scope){
$scope.registerData={};
})
.directive('confirmPwd',函数($interpolate,$parse){
返回{
要求:'ngModel',
链接:函数(范围、元素、属性、ngModelCtrl){
var pwdToMatch=$parse(attr.confirmPwd);
var pwdFn=$interpolate(属性确认Pwd)(范围);
范围$watch(pwdFn,函数(newVal){
ngModelCtrl.$setValidity('password',ngModelCtrl.$viewValue==newVal);
})
ngModelCtrl.$validators.password=函数(modelValue,viewValue){
var值=模型值| |视图值;
返回值==pwdToMatch(范围);
};
}
}
});

电子邮件
您没有输入电子邮件
密码
请输入密码
密码确认
需要密码确认
密码不同

最简单的方法是使用模式。对我来说很好

<input type="password" name="new_password1" ng-model="new_password1">

<input type="password" name="new_password2" ng-pattern="\b{{new_password1}}\b" ng-model="new_password2">
<div ng-messages="passwordForm.new_password2.$error">
    <div ng-message="pattern">Not equal!!!</div>
</div>

不平等!!!
我只使用了ionic(html)验证

新密码 需要新密码


确认密码
确认密码是必需的
密码不匹配..(来自印度的amar)
更新

在开发时,您可能需要创建自己的检查,这将影响表单的有效性。如果这些检查很简单,例如比较两个值,那么最好使用通用的指导原则,而不是针对每种情况编写自己的检查。看看指令

现场示范

angular.module('ExampleApp',['use','ngMessages']))
.controller('ExampleController',函数($scope){
});
。错误{
颜色:栗色
}

密码

确认密码 密码不匹配! 密码不匹配(来自ng消息)!
以下是我所做的(使用
ng模式)


ngMessage的工作原理是将$error.message_field_name添加到表单对象中的DOM字段名中(当然在范围内)。因此,如果您的DOM表单名为autentication\u form且DOM字段名为password\u confirmation,则需要将$scope.autentication\u form.password\u confirmation.$error.nomatch(或任何您想要的ngMessage名称)设置为true以显示“不匹配”错误

标记:

<input type="password" name="password_confirmation" ng-model="registerData.password_confirmation" required />

<div ng-messages="autentication_form.password_confirmation.$error">
                <div ng-message="required">Please repeat your password.</div>
                <div ng-message="nomatch">Doesn't match.</div>
            </div>
</div>

哎呀。刚刚注意到David的解决方案。为什么不调用$validate()而不是$setvalidation?逻辑已经存在。这看起来是最好、最简单的解决方案。我将稍微更改模式以匹配整行:
ng pattern=“^{{new_password1}}}$”
@Jared要修复解析词法错误,模式应使用/:
ng pattern=“/^{new_password1}}$/”
如果表达式的计算结果为字符串,请从角度页面读取API引用,然后,在将其包装为^和$字符后,它将被转换为RegExp。例如,“abc”将转换为新的RegExp(“^abc$”)。这意味着您可以使用ng pattern=“{new_password1}}@ebastuart当密码包含在regexp中有意义的字符(如*和+)时会发生什么情况?不要使用此选项,如果密码包含正则表达式有效字符,如/,$,则此答案将失败。
<input type="password" name="password_confirmation" ng-model="registerData.password_confirmation" required />

<div ng-messages="autentication_form.password_confirmation.$error">
                <div ng-message="required">Please repeat your password.</div>
                <div ng-message="nomatch">Doesn't match.</div>
            </div>
</div>
$scope.$watch("registerData.password + registerData.password_confirmation", function () {
            $scope.autentication_form.password_confirmation.$error.nomatch = $scope.registerData.password !== $scope.registerData.password_confirmation;
        });