mpassport.js 4.99 KB
/**
 * Created by liuziyang on 14-2-25.
 */
var _ruleMobile = /^(((13[0-9]{1})|(18[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
var _ruleEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
jQuery.validator.addMethod("isPhoneOrMail", function (value, element) {
    return this.optional(element) || (_ruleEmail.test(value) || _ruleMobile.test(value));
}, "账户必须是邮箱或手机.");
jQuery.validator.addMethod("isPhone", function (value, element) {
    return this.optional(element) || _ruleMobile.test(value);
}, "账户必须是手机号码.");
var insertAfterErrorMessages = false;
var signinSetTimeoutID;
function slideUpSetTimeout(){
    $('#errorContainer').slideUp();
    clearTimeout(signinSetTimeoutID);
}
function signin() {
    $("#signinForm").validate({
        showErrors: function (errorMap, errorList) {
            if (this.numberOfInvalids() == 0) {
                $('#errorContainer').slideUp();
            }else{
                var messages ='';
                if(errorMap.profile){
                    messages = errorMap.profile
                }else if(errorMap.password){
                    messages = errorMap.password
                }
                if(messages!=''){
                    $('#errorContainer').slideDown();
                    $('#errorMessages').text(messages);
                }
            }
        },
        success: function (label) {
            insertAfterErrorMessages = false;
        },
        rules: {
            profile: {
                required: true,
                isPhoneOrMail: true
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 20
            }
        },
        messages: {
            profile: {
                required: '邮箱或手机不能为空.',
                isPhoneOrMail: '账户必须是邮箱或手机号.'
            },
            password: {
                required: '密码不能为空',
                minlength: '密码太短最少6位',
                maxlength: '密码太长最多20位'
            }
        },
        onfocusout : false,
        onkeyup : false,
        onclick : false
    });
}

function register() {
    $("#registerForm").validate({
        showErrors: function (errorMap, errorList) {
            if (this.numberOfInvalids() == 0) {
                if (_ruleMobile.test($('#profile').val())) {
                    $('#registerForm').attr('action', '/passport/register/phone');
                }
            }
            if (this.numberOfInvalids() == 0) {
                $('#errorContainer').slideUp();
            }else{
                var messages ='';
                if(errorMap.profile){
                    messages = errorMap.profile
                }else if(errorMap.password){
                    messages = errorMap.password
                }else if(errorMap.confirm_password){
                    messages = errorMap.confirm_password
                }
                if(messages!=''){
                    $('#errorContainer').slideDown();
                    $('#errorMessages').text(messages);
                }
            }
        },
        success: function (label) {
            insertAfterErrorMessages = false;
        },
        rules: {
            profile: {
                required: true,
                isPhone: true
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 20
            },
            confirm_password: {
                required: true,
                equalTo: '#password'
            }
        },
        messages: {
            profile: {
                required: '手机号码不能为空.',
                isPhone: '账户必须是手机号.'
            },
            password: {
                required: '密码不能为空',
                minlength: '密码太短最少6位.',
                maxlength: '密码太长最多20位'
            },
            confirm_password: {
                required: '确认密码不能为空',
                equalTo: '确认密码和输入密码不相同.'
            }
        },
        onfocusout : false,
        onkeyup : false,
        onclick : false
    });
}