首页 > 解决方案 > 如果满足密码条件,则删除跨度

问题描述

我正在使用 span 显示密码规则。我想在满足输入条件时动态删除每个包含规则的跨度。我能够删除最小长度跨度,但不知道如何删除其他跨度。

这是我的 HTML 代码:

                    <input name="user_password" ng-minlength="8" ng-model="passForm.user_password1" id="user_password" type="password">
                    <label class="left-align" for="user_password">Password*</label>
                </div>
                <div class="input-field col s12">
                    <input name="user_password_confirm" ng-model="passForm.user_password2" id="user_password_confirm"
                           type="password" class="validate">
                    <span style="color:red" ng-if="passForm.user_password2 && passForm.user_password1 != passForm.user_password2">
                        <ul>Confirm password do not match.</ul>
                    </span>
                    <span style="color:red; display: block !important;">
                        Password should honor following rules:<br>
                    </span>
                    <span style="color:red; display: block !important;" ng-if="!passForm.user_password1 || passForm.user_password1.$error.minlength">
                        - minimun 8 charactes<br>
                    </span>    
                    <span style="color:red; display: block !important">
                        - atleast 1 uppercase character<br>
                        - atleast 1 lowercase character<br>
                        - atleast 1 digit<br>
                        - atleast 1 special character [@$!#%*?&.]<br>
                    </span>

我的控制器:

$scope.changeUserPassword = function(){
            var eFlag = false;
            $scope.errors = {};
            var rePassword = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&#.])[A-Za-z\d$@$!%*?&#.]{8,}/;
            if ($scope.passForm.user_password1 && rePassword.test($scope.passForm.user_password1) == false) {
                swal('Error!', "Please enter a password honoring rules in hint",'error');
                eFlag = true;
            }

标签: javascripthtmlangularjsregex

解决方案


我会说自己制定每条规则并相应地删除跨度标签。
那是

var rule1 = /^[^A-Z]*[A-Z]/;
if (rule1.test($scope.passForm.user_password1)) {
    // remove the span for the uppercase here
});

var rule2 = /^[^a-z]*[a-z]/;
if (rule2.test($scope.passForm.user_password1)) {
    // remove the span for the lowercase here
});

等等。将所有规则放在一个数组中并循环它们。


推荐阅读