﻿/*******************
调用
$(document).ready(function(){
    $('#ibtnReg').attr('group','r');
    $('#txtUserName').attr({
        group:'r',
        settype:"{type:'required|username',focuss:true,msg:'用户名不能为空|用户名必须为字母或数字'}"
    });
    $('#txtPassword').attr({
        group:'r',
        settype:"{type:'required|username|length',min:6,max:18,focuss:true,msg:'密码不能为空|密码必须为字母或数字|密码长度最小为6位最大为18位'}"
    });
    $('#txtPassword1').attr({
        group:'r',
        settype:"{type:'required|Compare',CompareId:'#txtPassword',CompareType:'!=',focuss:true,msg:'验证密码不能为空|验证密码与密码不一致，请检查。'}"
    });
    $('#txtTrueName').attr({
        group:'r',
        settype:"{type:'required',focuss:true,msg:'真实姓名不能并且与银行帐号开户人一致'}"
    });
    $('#txtContactWay').attr({
        group:'r',
        settype:"{type:'required',focuss:true,msg:'联系电话不能为空'}"
    });
    $('#txtMail').attr({
        group:'r',
        settype:"{type:'required|email',focuss:true,msg:'mail不能为空|mail格式有误请检查'}"
    });
    $('#ddlsProvince').attr({
        group:'r',
        settype:"{type:'required',focuss:true,msg:'请选择省份'}"
    });
    $('#ddlsGiroBanks').attr({
        group:'r',
        settype:"{type:'required',focuss:true,msg:'请选择转帐银行'}"
    });
    $('#txtOpeningBank').attr({
        group:'r',
        settype:"{type:'required',focuss:true,msg:'开户银行没有填写'}"
    });
    $('#txtBankAccount').attr({
        group:'r',
        settype:"{type:'required',focuss:true,msg:'银行帐号没有填写'}"
    });
    $('#txtAccountHolder').attr({
        group:'r',
        settype:"{type:'required|Compare',CompareId:'#txtTrueName',CompareType:'!=',focuss:true,msg:'开户人没有填写|开户人与真实姓名不一致，请检查。'}"
    });
    $('#txtIdentityCard').attr({
        group:'r',
        settype:"{type:'required|idcard',focuss:true,msg:'身份证没有填写|身份证的格式不正确'}"
    });
    $('#txtCode').attr({
        group:'r',
        settype:"{type:'required',focuss:true,msg:'验证码没有填写'}"
    });
});
*********************/
var inputs;
var regexEnum =
{
    number: '^[0-9]+$', //正整数

    email: "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", //邮件
    color: "^[a-fA-F0-9]{6}$", 			//颜色
    url: "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$", //url
    chinese: "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$", 				//仅中文
    ascii: "^[\\x00-\\xFF]+$", 			//仅ACSII字符
    zipcode: "^\\d{6}$", 					//邮编
    mobile: "^(13|15)[0-9]{9}$", 			//手机
    ip4: "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$", //ip地址
    required: "^\\S+$", 					//非空
    picture: "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$", //图片
    rar: "(.*)\\.(rar|zip|7zip|tgz)$", 							//压缩文件
    date: "^\\d{4}(\\-|\\/|\.)\\d{1,2}\\1\\d{1,2}$", 				//日期
    qq: "^[1-9]*[1-9][0-9]*$", 			//QQ号码
    tel: "^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$", //电话号码的函数(包括验证国内区号,国际区号,分机号)
    username: "^[a-zA-Z0-9_-]+$", 					//用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
    letter: "^[A-Za-z]+$", 				//字母
    letter_u: "^[A-Z]+$", 				//大写字母
    letter_l: "^[a-z]+$", 				//小写字母
    idcard: "^[1-9]([0-9]{14}|[0-9]{17})$"	//身份证
}
$(document).ready(function() {
    $('input[type=submit][group],input[type=image][group]').click(function() {
        setel = $(this).attr('group');
        var getlength = $("span[t='input'][group='" + setel + "']");
        var getthis;
        for (var i = 0; i < getlength.length; i++) {
            getthis = getlength.eq(i);
            eval('inputs=' + getthis.attr('settype').replace('inputs=', ''));
            var reg = new RegExp(inputs.type, 'i');
            getthis = $('#' + getthis.attr('ControlToValidate'));
            /****************************************/
            if (reg.test('required')) {//为空时执行
                if ($.trim(getthis.val()) == '') {
                    showmsgtype({ ainputs: inputs, getinput: getthis, zb: getzb({ type: inputs.type, tval: 'required' }) });
                    return false;
                }
            }
            /****************************************/
            if (reg.test('number')) {//正数（正整数 + 0）
                if (!new RegExp(regexEnum.number, 'i').test(getthis.val())) {
                    if (inputs.IsRequired == 'False' || getthis.val() != '') {
                        showmsgtype({ ainputs: inputs, getinput: getthis, zb: getzb({ type: inputs.type, tval: 'number' }) });
                        return false;
                    }
                }
            }
            /****************************************/
            if (reg.test('username')) {//用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
                if (!new RegExp(regexEnum.username, 'i').test(getthis.val())) {
                    if (inputs.IsRequired == 'False' || getthis.val() != '') {
                        showmsgtype({ ainputs: inputs, getinput: getthis, zb: getzb({ type: inputs.type, tval: 'username' }) });
                        return false;
                    }
                }
            }
            /****************************************/
            if (reg.test('length')) {//判断长度（必须存在min:int与max:int）两个属性
                if (inputs.min != undefined && inputs.max != undefined) {
                    if (getthis.val().length < inputs.min || getthis.val().length > inputs.max) {
                        if (inputs.IsRequired == 'False' || getthis.val() != '') {
                            showmsgtype({ ainputs: inputs, getinput: getthis, zb: getzb({ type: inputs.type, tval: 'length' }) });
                            return false;
                        }
                    }
                }
            }
            /****************************************/
            if (reg.test('Compare')) {//比较两个文本是否相等（必须存在CompareId:(jquery),CompareType:(string)）二个属性[CompareType该属性目前支持'=='、'!=']
                if (inputs.CompareId != 'NotSet') {
                    switch (inputs.CompareType) {
                        case "NotEqual":
                            if (getthis.val() != $("#" + inputs.CompareId).val()) {
                                if (inputs.IsRequired == 'False' || getthis.val() != '') {
                                    showmsgtype({ ainputs: inputs, getinput: getthis, zb: getzb({ type: inputs.type, tval: 'Compare' }) });
                                    return false;
                                }
                            }
                            break;
                        case "Equal":
                            if (getthis.val() == $("#" + inputs.CompareId).val()) {
                                if (inputs.IsRequired == 'False' || getthis.val() != '') {
                                    showmsgtype({ ainputs: inputs, getinput: getthis, zb: getzb({ type: inputs.type, tval: 'Compare' }) });
                                    return false;
                                }
                            }
                            break;
                    }
                }
            }
            /****************************************/
            if (reg.test('email')) {//判断email电子信箱
                if (!new RegExp(regexEnum.email, 'i').test(getthis.val())) {
                    if (inputs.IsRequired == 'False' || getthis.val() != '') {
                        showmsgtype({ ainputs: inputs, getinput: getthis, zb: getzb({ type: inputs.type, tval: 'email' }) });
                        return false;
                    }
                }
            }
            /****************************************/
            if (reg.test('idcard')) {//判断身份证号码
                if (!new RegExp(regexEnum.idcard, 'i').test(getthis.val())) {
                    if (inputs.IsRequired == 'False' || getthis.val() != '') {
                        showmsgtype({ ainputs: inputs, getinput: getthis, zb: getzb({ type: inputs.type, tval: 'idcard' }) });
                        return false;
                    }
                }
            }
            /****************************************/
            if (reg.test('Regex')) {//自定义正则符号
                if (!new RegExp(inputs.ValidationExpression, 'i').test(getthis.val())) {
                    if (inputs.IsRequired == 'False' || getthis.val() != '') {
                        showmsgtype({ ainputs: inputs, getinput: getthis, zb: getzb({ type: inputs.type, tval: 'Regex' }) });
                        return false;
                    }
                }
            }
        }
    });
});
function showmsgtype(p) {
    (p.ainputs.msg.split('|')[p.zb] != undefined) ? alert(p.ainputs.msg.split('|')[p.zb]) : '';
    (p.ainputs.focuss != undefined && p.ainputs.focuss) ? p.getinput.focus() : '';
}
function getzb(p) {//获取数组的坐标
    var cc = p.type.split('|');
    for (var c = 0; c < cc.length; c++) {
        if (cc[c] == p.tval) return c;
    }
}
