reset.js 6.03 KB
/**
 * 找回密码
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/12/14
 */

var $ = require('yoho-jquery');

var $pwd = $('#pwd'),
    $repwd = $('#re-input'),
    $next = $('#reset-pwd-btn'),
    $pwdErr = $('#pwd-err'),
    $repwdErr = $('#repwd-err'),
    $pwdTips = $('#pwd-tips');

var hasNoErrPw = false;

var $pwdIntensity = $('.pwd-intensity'),
    $pwdParent = $pwdIntensity.closest('.pwd-intensity-container'),
    $pwdTip1 = $('#pwd-tip1');

var pwdRegx = require('../common/mail-phone-regx').pwdValidateRegx;

require('yoho-jquery-placeholder');
require('../../simple-header');


/*
 * 计算密码复杂度
 */

function gettype(str, i) {
    if (str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57) {
        return 1;
    } else if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) {
        return 2;
    } else if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) {
        return 3;
    }

    return 4;
}

function isregular(cur, pre, type) {
    var curCode = cur.charCodeAt(0);
    var preCode = pre.charCodeAt(0);

    if (curCode - preCode === 0) {
        return true;
    }

    if (type !== 4 && (curCode - preCode === 1 || curCode - preCode === -1)) {
        return true;
    }

    return false;
}

function getcomplex(curType, preType) {
    if (preType === 0 || curType === preType) {
        return 0;
    } else if (curType === 4 || preType === 4) {
        return 2;
    } else {
        return 1;
    }
}

function computeComplex(password) {
    var complex = 0,
        length = password.length,
        pre = '',
        preType = 0,
        i = 0,
        cur,
        curType;


    for (i = 0; i < length; i++) {
        cur = password.charAt(i);
        curType = gettype(password, i);

        if (preType !== curType || !isregular(cur, pre, curType)) {
            complex += curType + getcomplex(curType, preType);
        }

        pre = cur;
        preType = curType;
    }

    return complex;
}

function pwdKeyupEvt() {
    var pwd = $pwd.val(),
        pwdStrength = computeComplex(pwd),
        level = 0;

    // TODO:自定义密码强度规则,需要修正
    if (pwdStrength === 0) {
        level = 0;
    } else if (pwdStrength <= 10) {
        level = 1;
    } else if (pwdStrength <= 20) {
        level = 2;
    } else {
        level = 3;
    }

    switch (level) {
        case 0:
            $pwdParent.removeClass('red yellow green');
            $pwdIntensity.removeClass('color');
            break;
        case 1:
            $pwdParent.addClass('red').removeClass('yellow green');
            $pwdIntensity.filter('.low').addClass('color');
            $pwdIntensity.filter('.mid,.high').removeClass('color');
            break;
        case 2:
            $pwdParent.addClass('yellow').removeClass('red green');
            $pwdIntensity.filter('.low,.mid').addClass('color');
            $pwdIntensity.filter('.high').removeClass('color');
            break;
        case 3:
            $pwdParent.addClass('green').removeClass('yellow red');
            $pwdIntensity.addClass('color');
            break;
        default:
            break;
    }

    // 提示框
    if (pwd === '') {
        $pwdTip1.removeClass('red yes no').addClass('default');
    } else if (pwd.length < 6 || pwd.length > 20) {
        $pwdTip1.removeClass('default yes').addClass('no red');
    } else {
        $pwdTip1.removeClass('default no red').addClass('yes');
    }

    if (pwdRegx.test(pwd)) {
        hasNoErrPw = true;
    } else {
        hasNoErrPw = false;
    }
}

// IE8 placeholder
$('input').placeholder();

$('.va').keyup(function() {
    var pass = true;

    if ($(this).hasClass('pwd')) {
        pwdKeyupEvt();
    } else {
        if ($(this).val() === '') {
            pass = false;
        }
    }
    if (pass && hasNoErrPw && $pwd.val() === $repwd.val()) {
        pass = true;
    } else {
        pass = false;
    }
    if (pass) {
        $next.removeClass('disable').prop('disabled', false);
    } else {
        $next.addClass('disable').prop('disabled', true);
    }
}).blur(function() {
    var $this = $(this),
        v = $this.val();

    if ($this.hasClass('pwd')) {
        if (v === '') {
            $this.addClass('error');
            $pwdErr.removeClass('hide').find('em').text('请输入密码');
        } else if (v.length < 6 || v.length > 20) {
            $this.addClass('error');
            $pwdErr.removeClass('hide').find('em').text('密码只支持6-20位');
        } else if (!pwdRegx.test(v)) {
            $this.addClass('error');
            $pwdErr.removeClass('hide').find('em').text('密码须字母和数字组合');
        } else {
            $pwdErr.addClass('hide');
            if ($repwd.val() !== '') {
                if (v !== $repwd.val()) {
                    $repwd.addClass('error');
                    $repwdErr.removeClass('hide').find('em').text('两次密码输入不一致,请重新输入');
                } else {
                    $repwd.removeClass('error');
                    $repwdErr.addClass('hide');
                }
            }
        }
    } else {
        if (v === '') {
            $this.addClass('error');
            $repwdErr.removeClass('hide').find('em').text('请输入密码确认');
        } else {
            if ($pwd.val() !== '' && v !== $pwd.val()) {
                $this.addClass('error');
                $repwdErr.removeClass('hide').find('em').text('两次密码输入不一致,请重新输入');
            } else {
                $this.removeClass('error');
                $repwdErr.addClass('hide');
            }
        }
    }
}).focus(function() {
    $(this).removeClass('error');

    // focus后错误提示隐藏
    if ($(this).hasClass('pwd')) {
        $pwdErr.addClass('hide');
    } else {
        $repwdErr.addClass('hide');
    }
});

$pwd.focus(function() {
    $pwdErr.addClass('hide');
    $pwdTips.removeClass('hide');
}).blur(function() {
    $pwdTips.addClass('hide');
});

$('#pwd, #repwd').keydown(function(e) {
    var code = e.keyCode || e.which;

    // 空格输入过滤
    if (code === 32) {
        e.preventDefault();
        return;
    }
});