admin管理员组文章数量:1394593
I'm trying to make an password field, that matches the following criteria:
- Valid password should be from 6 to 24 symbols long, and have at least one digit.
- When the field contains invalid password, it should always be have .ng-invalid class.
To make password symbols visible I use two inputs synchronized through model:
<form name="form" ng-app="myApp" ng-controller="Controller" novalidate>
<label>
Password:
<input type="password" name="uPass" maxlength="24" ng-hide="formFlags.showpass" ng-model="user.pass" ng-required validate-pass />
<input type="text" name="uPass" maxlength="24" ng-show="formFlags.showpass" ng-model="user.pass" ng-required validate-pass />
<span class="message" ng-show="form.uPass.$dirty && form.uPass.$invalid">Must be from 6 to 24 symbols long ang contain at least one digit.</span>
</label>
<label>
<input type="checkbox" name="uShowPass" ng-model="formFlags.showpass" toggle-pass/>Show password
</label>
</form>
See full code here: / .
Here I'm trying to do the following:
- I enter an invalid password. The input field gets.ng-invalid class (red border).
- I click "Show password". Password input gets hidden, text input gets visible.
The problem is that is not validated until I change it's value. How do I make it validate immediately after it is shown?
UPD: in AngularJS of versions 1.2.x and higher my example works properly, the issue is relevant only for lower versions.
I'm trying to make an password field, that matches the following criteria:
- Valid password should be from 6 to 24 symbols long, and have at least one digit.
- When the field contains invalid password, it should always be have .ng-invalid class.
To make password symbols visible I use two inputs synchronized through model:
<form name="form" ng-app="myApp" ng-controller="Controller" novalidate>
<label>
Password:
<input type="password" name="uPass" maxlength="24" ng-hide="formFlags.showpass" ng-model="user.pass" ng-required validate-pass />
<input type="text" name="uPass" maxlength="24" ng-show="formFlags.showpass" ng-model="user.pass" ng-required validate-pass />
<span class="message" ng-show="form.uPass.$dirty && form.uPass.$invalid">Must be from 6 to 24 symbols long ang contain at least one digit.</span>
</label>
<label>
<input type="checkbox" name="uShowPass" ng-model="formFlags.showpass" toggle-pass/>Show password
</label>
</form>
See full code here: http://jsfiddle/adept/9uCGQ/44/ .
Here I'm trying to do the following:
- I enter an invalid password. The input field gets.ng-invalid class (red border).
- I click "Show password". Password input gets hidden, text input gets visible.
The problem is that is not validated until I change it's value. How do I make it validate immediately after it is shown?
UPD: in AngularJS of versions 1.2.x and higher my example works properly, the issue is relevant only for lower versions.
Share Improve this question edited Jan 26, 2018 at 10:18 adept asked Jan 16, 2014 at 12:23 adeptadept 431 silver badge5 bronze badges 1- One thing to note is that the name of an input needs to be unique if you want to use it for field validation otherwise it will overwrite. You need to rename uPass for the text field and reference both of them in form.uPass.$dirty && form.uPass.$invalid – Liviu T. Commented Jan 16, 2014 at 14:53
1 Answer
Reset to default 6Just add formatters.unshift too, so it will also look to model changes, not only view ones.
Edited fiddle:
http://jsfiddle/9uCGQ/5/
Added code:
ctrl.$formatters.unshift(function(viewValue) {
if (viewValue.length >= scope.formFlags.minpasslength && PASS_REGEXP.test(viewValue)) {
// it is valid
ctrl.$setValidity('pass', true);
} else {
// it is invalid
ctrl.$setValidity('pass', false);
}
// update model anyway to sync password fields
return viewValue;
});
More about the two properties (parsers and formatters) on the ng-controller documentation: http://docs.angularjs/api/ng.directive:ngModel.NgModelController
本文标签: javascriptPassword field with validation and quotShow passwordquot option in AngularJSStack Overflow
版权声明:本文标题:javascript - Password field with validation and "Show password" option in AngularJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744096548a2590389.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论