【发布时间】:2016-10-05 06:04:24
【问题描述】:
您好,我正在尝试使用 ng-show 验证密码,即使密码正确也会显示错误消息,并且还会在页面刷新时显示。
我的代码是:表单名称是:registeruser
<!-- Set a password for the account -->
<div class="col-md-12" style="margin: 7px;">
<input type="password" id="password" name="password" style="height: 35px;" class="form-control input-lg" placeholder="Password" ng-model="register_password" required autocomplete="off" />
<!-- Button for viewing password -->
<input type="checkbox" id="eye" onclick="if(password.type=='text')password.type='password'; else password.type='text';" />
</div>
<!-- Error Message for password -->
<div class="error-message" ng-show="registeruser.password.$invalid">
Please enter at least 6 characters.
</div>
<div class="error-message" ng-show="registeruser.password.$pristine">
Please enter your password.
</div>
我用占位符名称、ID 和型号名称检查了 ng-show,但它不起作用。 此外,如果用户在未输入密码的情况下从密码字段中跳出,并且在密码字段为空时不在页面加载时,我希望显示密码错误。
【问题讨论】:
-
使用 ng-messages 进行表单验证。这是实现验证的简单且更好的方法,并且会为您进行大量 OOB 检查。下面的链接解释了yearofmoo.com/2014/05/how-to-use-ngmessages-in-angularjs.html