weak.js 3.17 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'),
    $titleTip = $('.title-tip'),
    $waakRepwdTip = $('.waak-repwd-tip'),
    $weakPwdPass = $('.weak-pwd-pass');

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

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

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

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

    if ($pwd.val() === '' || $repwd.val() === '') {
        pass = false;
    }

    if (pass && !$pwdErr.is(':hidden')) {
        pass = false;
    }

    if (pass && !$repwdErr.is(':hidden')) {
        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 === '') {
            $this.addClass('error');
            $repwdErr.removeClass('hide').find('em').text('请输入密码确认');
        } else if (v.length < 6 || v.length > 20) {
            $this.addClass('error');
            $repwdErr.removeClass('hide').find('em').text('密码只支持6-20位');
        } else if (!pwdRegx.test(v)) {
            $this.addClass('error');
            $repwdErr.removeClass('hide').find('em').text('密码须字母和数字组合');
        }

        $waakRepwdTip.find('span').addClass('hide');
    }

    $(this).trigger('keyup');
}).focus(function() {
    $(this).removeClass('error');

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

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

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

$next.on('click', function() {
    var refer = $weakPwdPass.data('href') || '/';
    var $this = $(this);

    // is frequent click
    if ($this.data('is-execu')) {
        return true;
    }

    $this.data('is-execu', true);

    $.post('/passport/back/modPwdByCodeAPI', {
        oldPwd: $pwd.val(),
        newPwd: $repwd.val()
    }, function(d) {
        $this.data('is-execu', false);

        if (d.code === 200) {
            document.location.href = '//www.yohobuy.com/passport/back/weakSuccess?refer=' + refer;
            return true;
        } else if (d.code === 402) {
            // token is error
            document.location.href = '//www.yohobuy.com/signin.html?refer=' + refer;
            return true;
        } else if (d.code === 401) {
            // token is error
            $pwdErr.removeClass('hide').find('em').text(d.message);
            return true;
        }

        $titleTip.html(d.message);
    });
});