专业编程基础技术教程

网站首页 > 基础教程 正文

javascript 正则表达式校验密码格式

ccvgpt 2024-07-21 17:28:49 基础教程 9 ℃

#正则表达式#

#javascript#

javascript 正则表达式校验密码格式

正则表达式

大写,小写,数字,英文特殊字符,至少满足三个
英文特殊字符范围:\x21-\x2f\x3a-\x40\x5b-\x60\x7B-\x7F

^(?!^[0-9a-z]+$)(?!^[0-9A-Z]+$)(?!^[0-9\x21-\x2f\x3a-\x40\x5b-\x60\x7B-\x7F]+$)(?!^[a-zA-Z]+$)(?!^[a-z\x21-\x2f\x3a-\x40\x5b-\x60\x7B-\x7F]+$)(?!^[A-Z\x21-\x2f\x3a-\x40\x5b-\x60\x7B-\x7F]+$)(?!^[A-Z\x21-\x2f\x3a-\x40\x5b-\x60\x7B-\x7F]+$)[a-z0-9A-Z\x21-\x2f\x3a-\x40\x5b-\x60\x7B-\x7F]+$

指定特殊字符范围:~!@#$%^&*()-_+?/

^(?![a-zA-Z]+$)(?![a-z0-9]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9~!@#$%^&*()\-_+?/]{8,12}$

快捷验证

oschina提供了在线工具,可以快速验证表达式。网址:
https://tool.oschina.net/regex/

基本概念

(pattern)

匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 '(' 或 ')'。

(?=pattern)

正向肯定预查(look ahead positive assert),在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,"Windows(?=95|98|NT|2000)"能匹配"Windows2000"中的"Windows",但不能匹配"Windows3.1"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。

(?!pattern)

正向否定预查(negative assert),在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如"Windows(?!95|98|NT|2000)"能匹配"Windows3.1"中的"Windows",但不能匹配"Windows2000"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。

bootstrapvalidator

静态文件引用:

<link rel="stylesheet" href="bootstrap.css"/>
<link rel="stylesheet" href="bootstrapValidator.min.css"/>
<script type="text/javascript" src="js/bootstrapValidator.min.js"></script>

定义form:

<form id="resetPwdForm" >
    <div class="form-group">
        <label for="oldPwd" class="">
            旧密码
        </label>
        <div class="">
            <input type="password" class="form-control" name="oldPwd"></input>
        </div>
    </div>
    <div class="form-group">
        <label for="newPwd" class="">
            新密码
        </label>
        <div class="">
            <input type="password" class="form-control" name="newPwd" id="newPwd"></input>
        </div>
    </div>
    <div class="form-group">
        <label for="againPwd" class="">
            确认新密码
        </label>
        <div class="">
            <input type="password" class="form-control" name="againPwd" id="againPwd"></input>
        </div>
    </div>
</form>

初始化:


    $(function () {
        $('resetPwdForm').bootstrapValidator({
                live: 'submitted', //enabled控件发生变化就验证。
        message: 'This value is not valid',
             feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                       },

            fields: {
                oldPwd: {
                    message: '旧密码验证失败',
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        }
                    }
                },
                newPwd: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                        stringLength: {
                            min: 8,
                            max: 12,
                            message: '长度8-12位'
                        },
                        regexp: {
                            regexp: ^(?![a-zA-Z]+$)(?![a-z0-9]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9~!@#$%^&*()_+?/]{8,12}$
                            message: 至少包含三种:大写字母、小写字母、数字、特殊字符(~!@#$%^&*()_+?/)
                        },
                        identical: {
                            field: 'againPwd',
                            message: '两次输入密码不一致'
                        }
                    }
                },
                againPwd: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                        stringLength: {
                            min: 8,
                            max: 12,
                            message: '长度8-12位'
                        },
                        regexp: {
                            regexp: ^(?![a-zA-Z]+$)(?![a-z0-9]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9~!@#$%^&*()_+?/]{8,12}$
                            message: 至少包含三种:大写字母、小写字母、数字、特殊字符(~!@#$%^&*()_+?/)
                        },
                        identical: {
                            field: 'newPwd',
                            message: '两次输入密码不一致'
                        }
                    }
                }
            }
        });
    });
    //newPwd 和againPwd 都需要定义identical

验证:

$('resetPwdForm').bootstrapValidator('validate')
if($('resetPwdForm').data('bootstrapValidator').isValid()){
    //验证通过,开始操作
} else {
    //验证不通过
}

最近发表
标签列表