非模式密码匹配

ng-pattern passwords match

您好,我正在创建注册表单,我想使用 angular 检查用户输入的密码是否匹配。我所有的验证工作(必需,最小和最大长度)但我总是收到密码不匹配的错误。

我的代码如下

<div class="control-group">
<label class="control-label">Password</label>
<div class="controls" ng-class="{ 'has-error': addCustomerForm.password_1.$touched && addCustomerForm.password_1.$invalid }">
    <input type="text" name="password_1" class="form-control input-lg" ng-model="newCustomerInfo.password_1" ng-minlength="4" ng-maxlength="12" required>
    <div ng-messages="addCustomerForm.password_1.$error" ng-if="addCustomerForm.password_1.$touched">
        <div ng-message="required">This field is required</div>
        <div ng-message="minlength">The password is too short.</div>
        <div ng-message="maxlength">The password is too long.</div>
    </div> 
</div>
</div>


<div class="control-group">
<label class="control-label">Password (Confirm)</label>
<div class="controls" ng-class="{ 'has-error': addCustomerForm.password_2.$touched && addCustomerForm.password_2.$invalid }">
    <input type="text" name="password_2" class="form-control input-lg" ng-model="newCustomerInfo.password_2" ng-minlength="4" ng-maxlength="12" ng-pattern="/^{{password_1}}$/" required>
    <div ng-messages="addCustomerForm.password_2.$error" ng-if="addCustomerForm.password_2.$touched">
        <div ng-message="required">This field is required</div>
        <div ng-message="minlength">The password is too short.</div>
        <div ng-message="maxlength">The password is too long.</div>
        <div ng-message="pattern">The passwords do not match.</div>
    </div> 
</div>
</div>

我认为问题可能出在 ng-pattern="/^{{password_1}}$/" 但我不知道我如何调试才能看到 ng-parent 的值。

任何帮助将不胜感激! 安德烈亚斯

您想使用 ng-pattern="{{newCustomerInfo.password_1}}"password_1 未定义,根据文档:

If the expression evaluates to a string, then it will be converted to a RegExp after wrapping it in ^ and $ characters. For instance, "abc" will be converted to new RegExp('^abc$') a.k.a /^abc$/.