reset.js 2.15 KB
/**
 * 找回密码设置密码
 * @author: TaoHuang
 * @date: 2016/7/18
 */

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

var $passwordInput = $('#pwd'),
    $repasswordInput = $('#re-input'),
    $next = $('#reset-pwd-btn');

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

function errTip(ele, msg) {
    var $errTip = ele.next('.tips');
    var $errMsg = $errTip.find('.content');

    $errMsg.text(msg);
    return $errTip.removeClass('hide');
}

function hideTip(ele) {
    return ele.next('.tips').addClass('hide');
}

require('yoho-jquery-placeholder');


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

function validatePassword() {
    var length = $passwordInput.val().length;

    if (length === 0) {
        errTip($passwordInput, '请输入密码');
        return false;
    }

    if (length < 6 || length > 20) {
        errTip($passwordInput, '密码只支持 6-20 位字符,建议字母+数字的组合');
        return false;
    }

    if (!pwdRegx.test($passwordInput.val())) {
        errTip($passwordInput, '密码只支持 6-20 位字符,建议字母+数字的组合');
        return false;
    }

    hideTip($passwordInput);
    return true;
}

$passwordInput.on('blur', function() {
    validatePassword();
}).on('focus', function() {
    $passwordInput.addClass('focus');
    hideTip($passwordInput);
}).on('blur', function() {
    $passwordInput.removeClass('focus');
});

function validateRepassword() {
    var length = $repasswordInput.val().length;

    if (length === 0) {
        errTip($repasswordInput, '请再次输入密码');
        return false;
    }

    if ($passwordInput.val() !== $repasswordInput.val()) {
        errTip($repasswordInput, '两次输入的密码不一致,请重新输入');
        return false;
    }

    hideTip($repasswordInput);
    return true;
}

$repasswordInput.on('blur', function() {
    validateRepassword();
}).on('focus', function() {
    $repasswordInput.addClass('focus');
    hideTip($repasswordInput);
}).on('blur', function() {
    $repasswordInput.removeClass('focus');
});

$next.click(function() {
    if (validatePassword() && validateRepassword()) {
        $('#reset-pwd-form').submit();
    }
});