api.js 3.18 KB
/**
 * 登录注册公用API
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/8
 */
var $ = require('jquery');

var trim = $.trim;

//邮箱验证规则
var emailRegx = /^([a-zA-Z0-9]+[_|\_|\.|-]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.|-]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;

//手机号码验证规则
var phoneRegx = {
    '+86': /^1[35847]{1}[0-9]{9}$/,
    '+852': /^[965]{1}[0-9]{7}$/,
    '+853': /^[0-9]{8}$/,
    '+886': /^[0-9]{10}$/,
    '+65': /^[98]{1}[0-9]{7}$/,
    '+60': /^1[1234679]{1}[0-9]{8}$/,
    '+1': /^[0-9]{10}$/,
    '+82': /^01[0-9]{9}$/,
    '+44': /^7[789][0-9]{8}$/,
    '+81': /^0[9|8|7][0-9]{9}$/,
    '+61': /^[0-9]{11}$/
};

/**
 * 密码显示隐藏
 * @params opt 初始化参数
 */
function bindEyesEvt(opt) {
    var $hasEye = $('.has-eye'),
        $eye;

    if (opt && opt.status === 'open') {
        $hasEye.append('<div class="eye"></div>');
    } else {
        $hasEye.append('<div class="eye close"></div>');
    }
    $eye = $hasEye.children('.eye');

    $eye.on('tap', function(e) {
        var $this = $(this),
            $pwd = $this.siblings('.pwd');

        e.preventDefault();
        $this.toggleClass('close');

        //切换密码显示和文本显示
        if ($this.hasClass('close')) {
            $pwd.attr('type', 'password');
        } else {
            $pwd.attr('type', 'text');
        }
        $pwd.focus();
    });
}

// 清空账号显示
function bindClearEvt() {
    var $hasClear = $('.has-clear'),
        $clear;

    $hasClear.append('<div class="clear-input"></div>');
    $clear = $hasClear.children('.clear-input');

    $clear.on('tap', function(e) {
        var $input = $clear.siblings('.input');

        $input.val('').trigger('input').focus();
        e.preventDefault();
    });

    //反向逻辑
    $hasClear.children('.input').bind('input', function() {
        var $this = $(this),
            $thisClear = $this.siblings('.clear-input'),
            val = trim($this.val());

        if (val === '') {
            $thisClear.hide();
        } else {
            $thisClear.show();
        }
    });
}

// 密码长度验证
function pwdValidate(pwd) {
    if (pwd.length >= 6 && pwd.length <= 20) {
        return true;
    }
    return false;
}

// hack for resolving direction:rtl didn't work in android uc
function selectCssHack($countrySelect) {
    var u = navigator.userAgent;

    function autoSelectWidth() {
        var wordCount = $countrySelect.find('option:selected').text().length;

        switch (wordCount) {

            //分别有2,3,4个汉字的情况
            case 2:
                $countrySelect.outerWidth(90);
                break;
            case 3:
                $countrySelect.outerWidth(110);
                break;
            default:
                $countrySelect.outerWidth(130);
        }
    }

    if (u.match(/uc/i) && u.match(/android/i)) {
        $countrySelect.change(function() {
            autoSelectWidth();
        });
    } else {
        $countrySelect.removeClass('in-android-uc');
    }
}

//Exports APIs
module.exports = {
    emailRegx: emailRegx,
    phoneRegx: phoneRegx,
    bindEyesEvt: bindEyesEvt,
    bindClearEvt: bindClearEvt,
    pwdValidate: pwdValidate,
    selectCssHack: selectCssHack
};