validate.js 7.45 KB
/**
 * 个人中心页-账号安全验证
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/02/23
 */
var $ = require('yoho.jquery');

var $checkUser = $('.check-user'),
    $checkInput = $checkUser.find('input').not('input[type=button],input[type=hidden]'),
    canSend = true,
    stime = 60,
    sInt;

// 身份校验
function checkForm(dom) {
    var val = dom.val(),
        len = val.length,
        $domParent = dom.parent(),
        $checkInfo = $domParent.find('.check-info'),
        inputName = dom.attr('name'),
        regular = '';

    $checkInfo.html('');

    if (inputName === 'oldpassword') {
        if (len === 0) {
            $checkInfo.html('<div class="form-error">密码不能为空!</div>');
            dom.addClass('input-error');
            return false;
        } else {
            $.get('/home/account/pass?pass=' + encodeURIComponent(val), function(data) {
                if (typeof data.code !== 'undefined' && data.code === 200) {
                    $checkInfo.html('<div class="form-success">&nbsp;</div>');
                    dom.removeClass('input-error');
                    return true;
                } else {
                    $checkInfo.html('<div class="form-error">密码错误!</div>');
                    dom.addClass('input-error');
                    return false;
                }
            });
        }
    }

    if (inputName === 'code') {
        $.get('/home/account/review?code=' + val, function(data) {
            if (typeof data.code !== 'undefined' && data.code === 200) {
                $checkInfo.html('<div class="form-success">&nbsp;</div>');
                dom.removeClass('input-error');
                return true;
            } else {
                $checkInfo.html('<div class="form-error">验证码错误!</div>');
                dom.addClass('input-error');
                return false;
            }
        });
    }

    if (inputName === 'newmail') {
        regular = '([a-zA-Z0-9]+)@([a-zA-Z0-9]+)[\.]([a-zA-Z0-9]+)';

        if (val.match(regular) === null) {
            $checkInfo.html('<div class="form-error">邮箱错误!</div>');
            dom.addClass('input-error');
            return false;
        } else {
            $.get('/home/account/mail?mail=' + val, function(data) {
                if (typeof data.code !== 'undefined' && data.code === 200) {
                    $checkInfo.html('<div class="form-success">&nbsp;</div>');
                    dom.removeClass('input-error');
                    return true;
                } else {
                    $checkInfo.html('<div class="form-error">邮箱已存在!</div>');
                    dom.addClass('input-error');
                    return false;
                }
            });

        }
    }

    if (inputName === 'mobile') {
        regular = '^1[35847]{1}[0-9]{9}';

        if (len === 0) {
            $checkInfo.html('<div class="form-error">手机号不能为空!</div>');
            dom.addClass('input-error');
            return false;
        } else if (val.match(regular) === null) {
            $checkInfo.html('<div class="form-error">手机号错误!</div>');
            dom.addClass('input-error');
            return false;
        } else {
            $.get('/home/mobile/exmobi?mobile=' + val, function(data) {
                if (typeof data.code !== 'undefined' && data.code !== 200) {
                    dom.addClass('input-error');
                    $checkInfo.html('<div class="form-error">手机号已经存在!</div>');
                    return false;
                } else {
                    dom.removeClass('input-error');
                    $checkInfo.html('<div class="form-success">&nbsp;</div>');
                    return true;
                }
            });
        }
    }

    if (inputName === 'password') {
        if (len < 6 || len > 20) {
            dom.addClass('input-error');
            $checkInfo.html('<div class="form-error">密码长度为6-20字符</div>');
            return false;
        } else {
            dom.removeClass('input-error');
            $checkInfo.html('<div class="form-success">&nbsp;</div>');
            return true;
        }
    }

    if (inputName === 'confirm_password') {
        if ($('#password').val() !== val) {
            dom.addClass('input-error');
            $checkInfo.html('<div class="form-error">两次密码不一致!</div>');
            return false;
        } else if ($('#password').val() !== '') {
            dom.removeClass('input-error');
            $('#password').next().html('<div class="form-success">&nbsp;</div>');
            $checkInfo.html('<div class="form-success">&nbsp;</div>');
            return true;
        }
    }

    return true;
}

// 校验表单
function checkAllForm() {
    var arr = [];

    $.each($checkInput, function(key, item) {
        arr.push(checkForm($(item)));
    });

    if (arr.indexOf(false) >= 0) {
        return false;
    } else {
        return true;
    }
}

// 切换验证码
function changeCode() {
    var timestamp = (new Date()).getTime();

    $('#the-code-img').attr('src', '/home/account/code?g=email_auth&time=' + timestamp);
}

// 重新发送倒计时
function code() {
    var sstring = '';

    if (stime > 0) {
        sstring = '重新发送' + stime + '秒';
        $('#sendButton').text(sstring);
        stime = stime - 1;
    } else {
        stime = 60;
        $('#sendButton').text('发送验证码');
        clearInterval(sInt);
        canSend = true;
    }
}

// 发送手机验证码
function sendcode() {
    var $mobile = $('#mobilevalue'),
        $code = $('#inputcode'),
        $mcheckInfo = $mobile.next(),
        mobileV = $mobile.val(),
        $ccheckInfo = $code.parent().find('check-info');

    if (canSend) {
        if (mobileV.match('^1[35847]{1}[0-9]{9}') === null) {
            $mcheckInfo.html('<div class="form-error">手机号错误!</div>');
            $mobile.addClass('input-error');
            return false;
        }
        $.get('/home/mobile/sendcode?mobile=' + mobileV, function(data) {
            if (typeof data.code !== 'undefined' && data.code === 200) {
                canSend = false;
                sInt = setInterval(function() {
                    code();
                }, 1000);
                $ccheckInfo.html('');
            } else {
                $ccheckInfo.html('<div class="form-error">验证码发送失败</div>');
            }
        });
    } else {
        return false;
    }
}

//验证完成后倒计时跳转
function toHome() {
    window.location.href = '/home/account';
}

$checkInput.blur(function() {
    checkForm($(this));
});

$('.sub-btn').on('click', function() {
    if (checkAllForm()) {
        $('#pwdform').submit();
    } else {
        return false;
    }
});

$('.the-code').on('click', function() {
    changeCode();
});

$('#send-mobile-code').on('click', function() {
    sendcode();
});

$('#inputcode').change(function() {
    var code = $('#inputcode').val();

    if (code !== '') {
        $.get('/home/mobile/review?code=' + code, function(data) {
            if (typeof data.code !== 'undefined' && data.code === 200) {
                $('#codetip').html('<div class="form-success">&nbsp;</div>');
            } else {
                $('#codetip').html('<div class="form-error">验证码错误!</div>');
            }
        });
    }
});

$(function() {
    var t = null;

    if ($('.res-info').length > 0) {
        t = setTimeout(function() {
            toHome();
        }, 5000);
    }

    changeCode();
});