gift.js 3.14 KB
/**
 * 个人中心页-兑换礼品卡
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/02/22
 */
var $ = require('yoho.jquery');

var $giftError = $('.giftCardCode').find('.gift-error'),
    $codeError = $('.captchaCode').find('.gift-error'),
    reg = '',
    code = '';

var Gift = {
    suc1: false,
    suc2: false,
    suc3: false,
    suc4: false,
    bindGiftCardForm: function() {
        $('#giftCardCode1').bind('blur keyup', function() {
            reg = /^[0-9a-zA-Z]{4,4}$/;

            if (!reg.test($('#giftCardCode1').val())) {
                $giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!');
                Gift.suc1 = false;
            } else {
                $giftError.html('');
                $('#giftCardCode2').focus();
                Gift.suc1 = true;
            }
        });

        $('#giftCardCode2').bind('blur keyup', function() {
            reg = /^[0-9a-zA-Z]{4,4}$/;

            if (!reg.test($('#giftCardCode2').val())) {
                $giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!');
                Gift.suc2 = false;
            } else {
                $giftError.html('');
                $('#giftCardCode3').focus();
                Gift.suc2 = true;
            }
        });

        $('#giftCardCode3').bind('blur keyup', function() {
            reg = /^[0-9a-zA-Z]{4,4}$/;

            if (!reg.test($('#giftCardCode3').val())) {
                $giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!');
                Gift.suc3 = false;
            } else {
                $giftError.html('');
                Gift.suc3 = true;
            }
        });

        $('#captchaCode').bind('blur keyup', function() {
            code = $('#captchaCode').val();

            if (code.length <= 0) {
                $codeError.html('请输入验证码!');
                Gift.suc4 = false;
            } else {
                $codeError.html('');
                Gift.suc4 = true;
            }
        });
    },

    checkForm: function() {
        if ($.trim($('#giftCardCode1').val()) === '' || $.trim($('#giftCardCode2').val()) === '' ||
            $.trim($('#giftCardCode3').val()) === '') {
            $giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!');
            return false;
        }

        if ($.trim($('#captchaCode').val()) === '') {
            $codeError.html('请输入验证码!');
            return false;
        }

        return true;
    }
};

//更换验证码
function refreshCaptcha() {
    var dt = new Date();

    $('#imgcode').attr('src', '/home/gift/captcha?t=' + dt.getTime());
    return false;
}

$('#sub-gift').on('click', function() {
    if (Gift.checkForm()) {
        $('#giftCardForm').submit();
    } else {
        return false;
    }
});

$(document).on('click', '#imgcode,.check-img', function() {
    refreshCaptcha();
});

$(function() {
    refreshCaptcha();
    Gift.bindGiftCardForm();
});