Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Created May 29, 2014 23:49
Show Gist options
  • Save jikeytang/a0b6306e70edc30ceedc to your computer and use it in GitHub Desktop.
Save jikeytang/a0b6306e70edc30ceedc to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140530-题目2
根据input输入情况,检验密码的强度,规则如下:
1. 纯数字,纯字母为:弱。
2. 数字与字母组合为:中。
3. 数字,字母,特殊字符@#$%^&*_+[]{}组合为:强。
4. 以上长度必为8位以上。
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@bee0060
Copy link

bee0060 commented May 30, 2014

function check(input)
{
    var regOnlyNumber = /^[0-9]{8,}$/,
        regOnlyNumberAndLetter = /(?!^[0-9]+$)(?!^[a-zA-Z]+$)^[0-9a-zA-Z]{8,}$/,
        regIncludeNumberAndLetter = /[0-9a-zA-Z]+/,
        regIncludeChars = /[@#$%^&*_+\[\]\{\}]+/,

        level = 0; // 1: simple; 2: normal; 3: high


    regOnlyNumber.test(input) && level = 1;
    regOnlyNumberAndLetter.test(input) && level = 2;
    regIncludeNumberAndLetter.test(input) &&
    regIncludeChars.test(input) &&
    input.length>=8 && 
    level = 3;

    return level;
}

@styling
Copy link

styling commented May 30, 2014

function test (element) {
    var elem = typeof element == "string" ? document.getElementById(element) : element;
    elem.onchange = function (event) {
        var arr = ["弱", "中", "强"]
        var val = this.value,
            b = 0;
        if(val.length < 9){
            alert("密码长度8位以上");
            return;
        }   
        if(/[0-9]/.test(val)){
            b++;
        }   
        if(/[a-zA-Z]/.test(val)){
            b++;
        }
        if(/[@#\$%\^&\*_\+\[\]\{\}]/.test(val)){
            b++;
        }
        alert("强度" + arr[b-1])
    }
}

这么写会不会有问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment