reg.js 8.72 KB
/*
 * @description 注册页js
 * @time 2015/12/14
 */

var $ = require('yoho.jquery'),
    regValidate = require('./mail-phone-regx');

var $registerPage = $('.register-page'),
    $pwdTips = $('#pwd-tips'),
    $errTip = $('#err-tip');

var $sendCaptcha = $('#send-captcha'),
    caCount = 4,
    validateResult = [];

var $pn = $('#phone-num'),
    $mc = $('#msg-captcha'),
    $pwd = $('#pwd'),
    $repwd = $('#repwd'),
    $ca = $('#captcha');

//signup验证
var $region = $('#country-code'),
    $regionSelect = $('#region');

setTimeout(function() {
    $pn.val('');
    $mc.val('');
    $pwd.val('');
    $repwd.val('');
    $ca.val('');
}, 100);

//验证码位数
$ca.attr('maxlength', caCount);

//密码规则提示
$pwd.focus(function() {
    $pwdTips.removeClass('hide');
}).blur(function() {
    $pwdTips.addClass('hide');
});


//IE8 placeholder
//$('[placeholder]').placeholder();

// 存储校验信息
validateResult = [
    {
        id: 'phone-num',
        message: '', //错误信息
        status: false, //当前的状态
        isNeedAjaxValidate: false //是否需要ajax校验,如果手机号正则校验通过则需要ajax校验该手机号是否注册过
    },
    {
        id: 'captcha',
        message: '',
        status: false,
        isNeedAjaxValidate: false //图形验证码需要发ajax确认是否正确
    },
    {
        id: 'msg-captcha',
        message: '',
        status: false,
        isNeedAjaxValidate: false //是否需要ajax请求验证短信验证码的正确性
    },
    {
        id: 'pwd',
        message: '',
        status: false
    },
    {
        id: 'repwd',
        message: '',
        status: false
    }
];

//手机号ajax校验
function phoneAjaxFn(callback) {
    $.ajax({
        url: '/passport/register/checkmobile',
        type: 'POST',
        data: {
            mobile: $pn.val(),
            area: $region.text().split('+')[1]
        }
    }).then(function(data) {
        switch (data.code) {
            case 200:
                validateResult[0].message = '';
                validateResult[0].status = true;
                break;
            case 404:
                validateResult[0].message = '改账号已经存在';
                validateResult[0].status = false;
                break;
        }

        callback();
    });
}

//图形验证码ajax校验
function picCaptchaAjaxFn(callback) {
    $.ajax({
        type: 'POST',
        url: '/passport/register/piccaptcha',
        data: {
            code: $ca.val(),
            mobile: $pn.val(),
            area: $region.text().split('+')[1]
        }
    }).then(function(data) {
        switch (data.code) {
            case 200:
                validateResult[1].message = '';
                validateResult[1].status = true;
                break;
            case 404:
                validateResult[1].message = '图形验证码错误';
                validateResult[1].status = false;
                break;
        }
        callback();
    });
}

//短信验证码ajax校验
function msgCaptchaAjaxFn(callback) {
    $.ajax({
        type: 'POST',
        url: '/passport/register/msgcaptcha',
        data: {
            code: $ca.val(),
            mobile: $pn.val(),
            area: $region.text().split('+')[1]
        }
    }).then(function(data) {
        switch (data.code) {
            case 200:
                validateResult[2].message = '';
                validateResult[2].status = true;
                break;
            case 404:
                validateResult[2].message = '短信验证码错误';
                validateResult[2].status = false;
                break;
        }

        callback();
    });
}

// 验证
function validateRule($element, callback) {

    var val = $.trim($element.val()),
        regionCode;


    //手机号校验
    if ($element.hasClass('phone-num')) {

        regionCode = $region.text();

        if (val === '') {
            validateResult[0].message = '请输入手机号码';
            validateResult[0].status = false;
            callback();

        } else if (!regValidate.phoneRegx[regionCode].test(val)) {
            validateResult[0].message = '手机号码格式不正确,请重新输入';
            validateResult[0].status = false;
            callback();

        } else {
            validateResult[0].message = '';
            validateResult[0].status = false;
            validateResult[0].isNeedAjaxValidate = true;

            phoneAjaxFn(callback);
        }

    //图形验证码校验
    } else if ($element.hasClass('captcha')) {
        if (val === '') {
            validateResult[1].message = '请输入图形验证码';
            validateResult[1].status = false;
            callback();

        } else {
            validateResult[1].message = '';
            validateResult[1].isNeedAjaxValidate = true;
            picCaptchaAjaxFn(callback);
        }

    //短信验证码校验
    } else if ($element.hasClass('msg-captcha')) {

        if (val === '') {
            validateResult[2].message = '请输入短信验证码';
            validateResult[2].status = false;
            callback();

        } else {
            validateResult[2].message = '';
            validateResult[2].status = false;
            validateResult[2].isNeedAjaxValidate = true;
            msgCaptchaAjaxFn(callback);
        }

    //密码校验
    } else if ($element.hasClass('pwd')) {
        if (val === '') {
            validateResult[3].message = '请输入密码';
            validateResult[3].status = false;

        } else if (val.length < 6 || val.length > 20) {
            validateResult[3].message = '密码只支持6-20位字符';
            validateResult[3].status = false;

        } else if (/\s/.test($element.val())) {
            validateResult[3].message = '密码不能包含空格';
            validateResult[3].status = false;

        } else {
            validateResult[3].message = '';
            validateResult[3].status = true;
        }
        callback();

    //二次密码校验
    } else if ($element.hasClass('repwd')) {
        if (val === '') {
            validateResult[4].message = '请输入密码确认';
            validateResult[4].status = false;

        } else if ($pwd.val() !== val) {
            validateResult[4].message = '与密码不一致,请重新输入';
            validateResult[4].status = false;

        } else {
            validateResult[4].message = '';
            validateResult[4].status = true;

        }

        callback();
    }
}

//显示提示信息
function showErrTip() {
    var show = false,
        tipPosition,
        $container,
        i,
        validateResultLen = validateResult.length;

    for (i = 0; i < validateResultLen; i++) {
        if (!show && !!validateResult[i].message) {

            //显示错误提示
            $errTip.find('span').text(validateResult[i].message);

            $container = $('#' + validateResult[i].id);

            tipPosition = $container.offset();
            $errTip.css({
                top: tipPosition.top - 40,
                left: tipPosition.left
            }).removeClass('hide');

            show = true; //停止判断
        }
    }
}

//显示红色边框
function showBorder() {

    var $errInput,
        i,
        validateResultLen = validateResult.length;

    for (i = 0; i < validateResultLen; i++) {
        if (!!validateResult[i].message) {

            //显示红色边框
            $errInput = $('#' + validateResult[i].id);
            $errInput.addClass('error');

        }
    }
}

// 失去焦点时开始校验
$registerPage.find('.va').focus(function() {

    // 去掉错误提示,当获得焦点的时候重置message信息
    var index = $(this).parents('li').attr('data-index');

    validateResult[index].message = '';

    showErrTip(); // 显示错误提示


}).blur(function() {

    validateRule($(this), function() {
        showErrTip();
        showBorder(); // 显示红色边框
    });
});

$regionSelect.change(function() {

    $region.text('+' + $('#region').val());

    validateRule($pn, showErrTip); //验证
});

// 图形验证keyup
$ca.keyup(function() {

    // 验证码输入4位并且手机号验证通过发ajax请求
    if ($(this).val().length === 4 && validateResult[0].status) {

        picCaptchaAjaxFn(function() {
            if (validateResult[1].status) {
                $sendCaptcha.removeClass('disable');
            }
        });

    } else {
        $sendCaptcha.addClass('disable');
    }
});