1

嗨,我正在创建一个注册表单,我想使用 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 的值。

任何帮助都感激不尽!安德烈亚斯。

4

1 回答 1

4

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

^如果表达式的计算结果为字符串,则在将其包装在和$字符中后,它将被转换为正则表达式。例如,"abc"将转换为new RegExp('^abc$')aka /^abc$/

于 2016-02-05T11:19:31.573 回答