基于Jquery的标签智能验证实现代码_jquery_脚本之家

后经过一段对Jquery的学习,Jquery的强大解决了辅助代码过多不易维护的问题。AutoValidate.JS
复制代码 代码如下: /// //验证方法
v1.0,创建于2010-12-9 完成2010-12-16 MR.X 制
//修改2010-12-10、2010-12-12、2010-12-15、2010-12-16添入信息提示动画效果
//支持 type=text type=checkbox type=radio select 标签验证
//vld=”***”必填 格式验证 //vld=”n***”选填 格式验证
//err=”***”错误显示内容
//super=”y”用追加提示信息,要用y以外字母得修改同级一组验证,同级一组的标签可以在第一个标签进行super=”y”属性标识,其它不用
//len=”***”长度限制,用于textarea标签 // $ { //正则匿名对象 var
strRegex = {}; //错误信息匿名对象 var strError = {}; //正确信息匿名对象
var strRight = {}; /** 参数配置 start **/ //非空 strRegex.NoNull =
/[^\s]+/; strError.NoNull = “请填写内容,如123、中国!”; //邮箱
strRegex.Email = /^+@+({1,2})$/; strError.Email =
“请核对邮箱格式,如china@163.com!”; //网址 strRegex.Url =
/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\’:+!]**$/;
strError.Url = “请核对网址格式,如//www.jb51.net!”; //账号 strRegex.An
= /^{6,16}$/; strError.An = “请核对账号格式,如china_56!”; //数字
strRegex.Math = /\d+$/; strError.Math = “请核对数字格式,如1234!”;
//年龄 strRegex.Age = /^\d{2}$/; strError.Age =
“请核对年龄格式,10~99岁之间!”; //邮编 strRegex.Post =
/^[1-9]\d{5}$/; strError.Post = “请核对邮编格式,如150001!”; //电话
strRegex.Phone =
/^|-|–(\d{4}|\d{3}|\d{2}|\d{1})|-(\d{4}|\d{3}|\d{2}|\d{1})))$/;
strError.Phone = “请核对电话格式,如15546503251!”; //身份证
strRegex.Card =
/^(([1-9]\d{5}[1-9]\d{3}|([1-9]\d{5}[1-9]\d{1}$/;
strError.Card = “请核对身份证格式,如230103190001010000!”; //金钱
strRegex.Price = /^?$/; strError.Price = “请核对金钱格式,如99.98!”;
//日期 strRegex.Date =
/|(3[01]|[12][0-9]|0?[1-9])$)||$)||(2[0-8]|1[0-9]|0?[1-9])$)||||||(^([2-9][0-9][2468][048])||(^([2-9][0-9][13579][26])/;
strError.Date = “请核对日期格式,如1999.9.9、1999-9-9、1999.09.09!”;
//时间 strRegex.Time = /^([0-9]|[0-1][0-9]|[2][0-3])$/;
strError.Time = “请核对时间格式,如23:59!”; strError.Length =
“请核对输入信息长度,长度小于”; strRight.Info = “格式正确!”;
//可以设置为空 //下拉框 strRegex.DDL = “请选择”; strError.DDL =
“请选择选项”; //单个checkbox复选框 strRegex.Check = “请选择”;
strError.Check = “请选择选项”; //单个radio复选框 strRegex.Radio =
“请选择”; strError.Radio = “请选择选项”; //同级一组checkbox复选框
strRegex.CheckGroup = “请选择”; strError.CheckGroup = “请选择选项”;
//同级一组radio复选框 strRegex.RadioGroup = “请选择”;
strError.RadioGroup = “请选择选项”; //在标签后面追加信息 var SpanError =
图片 1%20+%20%22images/error.gif)”;
var SpanOk = “图片 2%20+%20%22images/ok.gif)”;
/** 参数配置 end **/ /** Main **/ //文件目录,回返最顶级目录 ../
function FilePath() { var file = “”; var path =
window.location.pathname.split.each { file = “../” + file; }); return
file; } //页验证自检 $.blur { RegexGether; //验证处理集合 function
RegexGether { switch { case “nonull”: RegexNull; break; case “age”:
RegexInputTextAll($ctrl, strRegex.Age, strError.Age); break; case
“nage”: RegexInputTextOnly($ctrl, strRegex.Age, strError.Age); break;
case “date”: RegexInputTextAll($ctrl, strRegex.Date, strError.Date);
break; case “ndate”: RegexInputTextOnly($ctrl, strRegex.Date,
strError.Date); break; case “price”: RegexInputTextAll($ctrl,
strRegex.Price, strError.Price); break; case “nprice”:
RegexInputTextOnly($ctrl, strRegex.Price, strError.Price); break; case
“email”: RegexInputTextAll($ctrl, strRegex.Email, strError.Email);
break; case “nemail”: RegexInputTextOnly($ctrl, strRegex.Email,
strError.Email); break; case “post”: RegexInputTextAll($ctrl,
strRegex.Post, strError.Post); break; case “npost”:
RegexInputTextOnly($ctrl, strRegex.Post, strError.Post); break; case
“card”: RegexInputTextAll($ctrl, strRegex.Card, strError.Card); break;
case “ncard”: RegexInputTextOnly($ctrl, strRegex.Card, strError.Card);
break; case “time”: RegexInputTextAll($ctrl, strRegex.Time,
strError.Time); break; case “ntime”: RegexInputTextOnly($ctrl,
strRegex.Time, strError.Time); break; case “math”:
RegexInputTextAll($ctrl, strRegex.Math, strError.Math); break; case
“nmath”: RegexInputTextOnly($ctrl, strRegex.Math, strError.Math); break;
case “url”: RegexInputTextAll($ctrl, strRegex.Url, strError.Url); break;
case “nurl”: RegexInputTextOnly($ctrl, strRegex.Url, strError.Url);
break; case “an”: RegexInputTextAll($ctrl, strRegex.An, strError.An);
break; case “nan”: RegexInputTextOnly($ctrl, strRegex.An, strError.An);
break; case “phone”: RegexInputTextAll($ctrl, strRegex.Phone,
strError.Phone); break; case “nphone”: RegexInputTextOnly($ctrl,
strRegex.Phone, strError.Phone); break; case “ddl”: RegexSelect; break;
case “check”: RegexInputCheckBoxRadioOnly($ctrl, strError.Check); break;
case “radio”: RegexInputCheckBoxRadioOnly($ctrl, strError.Radio); break;
case “checkgroup”: RegexInputCheckBoxRadioAll($ctrl,
strError.CheckGroup); break; case “radiogroup”:
RegexInputCheckBoxRadioAll($ctrl, strError.RadioGroup); break; } }
//标签内容空验证 function RegexNull { if (strRegex.NoNull.test { return
RegexLen; } else { Error($ctrl, strError.NoNull); return false; } }
//验证多个同级一组input标签 或 input标签 function
RegexInputCheckBoxRadioAll { if .children.length == 0) { if .children {
//同级标签中可能会有多个[super=’y’]存在,只取一个,用return
false;控制,执行一次就从循环体中跳出 $ctrl.parent().children.each {
Error; return false; }); } else { //同级一组标签一起报错
$ctrl.parent().children(“[type='” + $ctrl.attr.each { Error; } return
false; } else { if .children { $ctrl.parent().children.each { Ok; return
false; }); } else { $ctrl.parent.each { Ok; } return true; } }
//验证单个input标签 或 input标签 function RegexInputCheckBoxRadioOnly {
if (!$ctrl.attr { Error; return false; } else { Ok; return true; } }
//select标签,选项验证 function RegexSelect { if == strRegex.DDL) {
Error; return false; } else { Ok; return true; } }
//标签允许为空内容格式验证,type=text标签验证 function
RegexInputTextOnly { //先验证标签是否为空 if (strRegex.NoNull.test {
return RegexOtherFormat; } else { Ok; return true; } }
//标签不允许为空内容格式验证,type=text标签验证 function
RegexInputTextAll { //先验证标签是否为空 if (strRegex.NoNull.test {
return RegexOtherFormat; } else { Error; return false; } }
//标签内容格式验证 function RegexOtherFormat { if (Regex.test { return
RegexLen; } else { Error; return false; } } //textarea标签长度验证
function RegexLen { //标签中是否有len属性 if { var error =
strError.Length + $ctrl.attr + “字!”; if .length) > parseInt {
Error; return false; } } Ok; return true; } //格式验证错误显示 function
Error { //标签中有err属性,根据属性内容填入title中 if { error =
$ctrl.attr; } if { //具有super属性标签追加span
$ctrl.parent.remove.append; $ctrl.parent.append; $ctrl.parent.fadeTo; }
else { //追加到title属性中 $ctrl.attr; $ctrl.addClass; } }
//格式验证无误,清除错误样式 function Ok { //去除可以存在的错误样式 if {
$ctrl.parent.remove(); } else { $ctrl.removeClass; } if .length > 0)
{ var right = strRight.Info; //检查标签中是否有rig属性 if { right =
$ctrl.attr; } //添加正确提示信息 if { $ctrl.parent; $ctrl.parent.append;
$ctrl.parent.fadeTo.fadeTo.fadeTo; } else { $ctrl.attr; } } } //提交自检
$.click { $.each { RegexGether; //检查标签中样式是否有error if
($(“#form1 [vld][class=’error’]”).length > 0) { return false; }
else { return true; } }); }); AutoValidate.htm 复制代码 代码如下:

jquery表单验证源码,jquery表单源码

/**
数据验证完整性
**/
$.fn.Validform = function () {
    var Validatemsg = “”;
    var Validateflag = true;
    $(this).find(“[isvalid=yes]”).each(function () {
        var checkexpession = $(this).attr(“checkexpession”);
        var errormsg = $(this).attr(“errormsg”);
        if (checkexpession != undefined) {
            if (errormsg == undefined) {
                errormsg = “”;
            }
            var value = $(this).val();
            if ($(this).hasClass(‘ui-select’)) {
                value = $(this).attr(‘data-value’);
            }
            switch (checkexpession) {
                case “NotNull”:
                    {
                        if (isNotNull(value)) {
                            Validatemsg = errormsg + “不能为空!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “Num”:
                    {
                        if (!isInteger(value)) {
                            Validatemsg = errormsg +
“必须为数字!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “NumOrNull”:
                    {
                        if (!isIntegerOrNull(value)) {
                            Validatemsg = errormsg +
“必须为数字!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “Email”:
                    {
                        if (!isEmail(value)) {
                            Validatemsg = errormsg +
“必须为E-mail格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “EmailOrNull”:
                    {
                        if (!isEmailOrNull(value)) {
                            Validatemsg = errormsg +
“必须为E-mail格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “EnglishStr”:
                    {
                        if (!isEnglishStr(value)) {
                            Validatemsg = errormsg +
“必须为字符串!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “EnglishStrOrNull”:
                    {
                        if (!isEnglishStrOrNull(value)) {
                            Validatemsg = errormsg +
“必须为字符串!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “LenNum”:
                    {
                        if (!isLenNum(value, $(this).attr(“length”)))
{
                            Validatemsg = errormsg + “必须为” +
$(this).attr(“length”) + “位数字!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “LenNumOrNull”:
                    {
                        if (!isLenNumOrNull(value,
$(this).attr(“length”))) {
                            Validatemsg = errormsg + “必须为” +
$(this).attr(“length”) + “位数字!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “LenStr”:
                    {
                        if (!isLenStr(value, $(this).attr(“length”)))
{
                            Validatemsg = errormsg + “必须小于” +
$(this).attr(“length”) + “位字符!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “LenStrOrNull”:
                    {
                        if (!isLenStrOrNull(value,
$(this).attr(“length”))) {
                            Validatemsg = errormsg + “必须小于” +
$(this).attr(“length”) + “位字符!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “Phone”:
                    {
                        if (!isTelephone(value)) {
                            Validatemsg = errormsg +
“必须电话格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “PhoneOrNull”:
                    {
                        if (!isTelephoneOrNull(value)) {
                            Validatemsg = errormsg +
“必须电话格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “Fax”:
                    {
                        if (!isTelephoneOrNull(value)) {
                            Validatemsg = errormsg +
“必须为传真格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “Mobile”:
                    {
                        if (!isMobile(value)) {
                            Validatemsg = errormsg +
“必须为手机格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “MobileOrNull”:
                    {
                        if (!isMobileOrnull(value)) {
                            Validatemsg = errormsg +
“必须为手机格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “MobileOrPhone”:
                    {
                        if (!isMobileOrPhone(value)) {
                            Validatemsg = errormsg +
“必须为电话格式或手机格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “MobileOrPhoneOrNull”:
                    {
                        if (!isMobileOrPhoneOrNull(value)) {
                            Validatemsg = errormsg +
“必须为电话格式或手机格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “Uri”:
                    {
                        if (!isUri(value)) {
                            Validatemsg = errormsg +
“必须为网址格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “UriOrNull”:
                    {
                        if (!isUriOrnull(value)) {
                            Validatemsg = errormsg +
“必须为网址格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “Equal”:
                    {
                        if (!isEqual(value, $(this).attr(“eqvalue”)))
{
                            Validatemsg = errormsg + “不相等!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “Date”:
                    {
                        if (!isDate(value, $(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为日期格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “DateOrNull”:
                    {
                        if (!isDateOrNull(value,
$(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为日期格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “DateTime”:
                    {
                        if (!isDateTime(value, $(this).attr(“eqvalue”)))
{
                            Validatemsg = errormsg +
“必须为日期时间格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “DateTimeOrNull”:
                    {
                        if (!isDateTimeOrNull(value,
$(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为日期时间格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “Time”:
                    {
                        if (!isTime(value, $(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为时间格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “TimeOrNull”:
                    {
                        if (!isTimeOrNull(value,
$(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为时间格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “ChineseStr”:
                    {
                        if (!isChinese(value, $(this).attr(“eqvalue”)))
{
                            Validatemsg = errormsg +
“必须为中文!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “ChineseStrOrNull”:
                    {
                        if (!isChineseOrNull(value,
$(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为中文!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “Zip”:
                    {
                        if (!isZip(value, $(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为邮编格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “ZipOrNull”:
                    {
                        if (!isZipOrNull(value,
$(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为邮编格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “Double”:
                    {
                        if (!isDouble(value, $(this).attr(“eqvalue”)))
{
                            Validatemsg = errormsg +
“必须为小数!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “DoubleOrNull”:
                    {
                        if (!isDoubleOrNull(value,
$(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为小数!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “IDCard”:
                    {
                        if (!isIDCard(value, $(this).attr(“eqvalue”)))
{
                            Validatemsg = errormsg +
“必须为身份证格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “IDCardOrNull”:
                    {
                        if (!isIDCardOrNull(value,
$(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为身份证格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “IsIP”:
                    {
                        if (!isIP(value, $(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为IP格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                case “IPOrNull”:
                    {
                        if (!isIPOrNullOrNull(value,
$(this).attr(“eqvalue”))) {
                            Validatemsg = errormsg +
“必须为IP格式!\n”;
                            Validateflag = false;
                            ValidationMessage($(this), Validatemsg);
return false;
                        }
                        break;
                    }
                default:
                    break;
            }
        }
    });
    if ($(this).find(“[fieldexist=yes]”).length > 0) {
        return false;
    }
    return Validateflag;
    //验证不为空 notnull
    function isNotNull(obj) {
        obj = $.trim(obj);
        if (obj.length == 0 || obj == null || obj == undefined) {
            return true;
        }
        else
            return false;
    }
    //验证数字 num
    function isInteger(obj) {
        reg = /^[-+]?\d+$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证数字 num  或者null,空
    function isIntegerOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        reg = /^[-+]?\d+$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //Email验证 email
    function isEmail(obj) {
        reg = /^\w{3,}@\w+(\.\w+)+$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //Email验证 email   或者null,空
    function isEmailOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        reg = /^\w{3,}@\w+(\.\w+)+$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证只能输入英文字符串 echar
    function isEnglishStr(obj) {
        reg = /^[a-z,A-Z]+$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证只能输入英文字符串 echar 或者null,空
    function isEnglishStrOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        reg = /^[a-z,A-Z]+$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证是否是n位数字字符串编号 nnum
    function isLenNum(obj, n) {
        reg = /^[0-9]+$/;
        obj = $.trim(obj);
        if (obj.length > n)
            return false;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证是否是n位数字字符串编号 nnum或者null,空
    function isLenNumOrNull(obj, n) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        reg = /^[0-9]+$/;
        obj = $.trim(obj);
        if (obj.length > n)
            return false;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证是否小于等于n位数的字符串 nchar
    function isLenStr(obj, n) {
        //reg = /^[A-Za-z0-9\u0391-\uFFE5]+$/;
        obj = $.trim(obj);
        if (obj.length == 0 || obj.length > n)
            return false;
        else
            return true;
    }
    //验证是否小于等于n位数的字符串 nchar或者null,空
    function isLenStrOrNull(obj, n) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        obj = $.trim(obj);
        if (obj.length > n)
            return false;
        else
            return true;
    }
    //验证是否电话号码 phone
    function isTelephone(obj) {
        reg = /^(\d{3,4}\-)?[1-9]\d{6,7}$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证是否电话号码 phone或者null,空
    function isTelephoneOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        reg = /^(\d{3,4}\-)?[1-9]\d{6,7}$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证是否手机号 mobile
    function isMobile(obj) {
        reg = /^(\+\d{2,3}\-)?\d{11}$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证是否手机号 mobile或者null,空
    function isMobileOrnull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        reg = /^(\+\d{2,3}\-)?\d{11}$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证是否手机号或电话号码 mobile phone
    function isMobileOrPhone(obj) {
        reg_mobile = /^(\+\d{2,3}\-)?\d{11}$/;
        reg_phone = /^(\d{3,4}\-)?[1-9]\d{6,7}$/;
        if (!reg_mobile.test(obj) && !reg_phone.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证是否手机号或电话号码 mobile phone或者null,空
    function isMobileOrPhoneOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        reg = /^(\+\d{2,3}\-)?\d{11}$/;
        reg2 = /^(\d{3,4}\-)?[1-9]\d{6,7}$/;
        if (!reg.test(obj) && !reg2.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证网址 uri
    function isUri(obj) {
        reg =
/^http:\/\/[a-zA-Z0-9]+\.[a-zA-Z0-9]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\”\”])*$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证网址 uri或者null,空
    function isUriOrnull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        reg =
/^http:\/\/[a-zA-Z0-9]+\.[a-zA-Z0-9]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\”\”])*$/;
        if (!reg.test(obj)) {
            return false;
        } else {
            return true;
        }
    }
    //验证两个值是否相等 equals
    function isEqual(obj1, controlObj) {
        if (obj1.length != 0 && controlObj.length != 0) {
            if (obj1 == controlObj)
                return true;
            else
                return false;
        }
        else
            return false;
    }
    //判断日期类型是否为YYYY-MM-DD格式的类型 date
    function isDate(obj) {
        if (obj.length != 0) {
            reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
            if (!reg.test(obj)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断日期类型是否为YYYY-MM-DD格式的类型 date或者null,空
    function isDateOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        if (obj.length != 0) {
            reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
            if (!reg.test(obj)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断日期类型是否为YYYY-MM-DD hh:mm:ss格式的类型 datetime
    function isDateTime(obj) {
        if (obj.length != 0) {
            reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})
(\d{1,2}):(\d{1,2}):(\d{1,2})$/;
            if (!reg.test(obj)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断日期类型是否为YYYY-MM-DD hh:mm:ss格式的类型
datetime或者null,空
    function isDateTimeOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        if (obj.length != 0) {
            reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})
(\d{1,2}):(\d{1,2}):(\d{1,2})$/;
            if (!reg.test(obj)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断日期类型是否为hh:mm:ss格式的类型 time
    function isTime(obj) {
        if (obj.length != 0) {
            reg =
/^((20|21|22|23|[0-1]\d)\:[0-5][0-9])(\:[0-5][0-9])?$/;
            if (!reg.test(obj)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断日期类型是否为hh:mm:ss格式的类型 time或者null,空
    function isTimeOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        if (obj.length != 0) {
            reg =
/^((20|21|22|23|[0-1]\d)\:[0-5][0-9])(\:[0-5][0-9])?$/;
            if (!reg.test(obj)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断输入的字符是否为中文 cchar
    function isChinese(obj) {
        if (obj.length != 0) {
            reg = /^[\u0391-\uFFE5]+$/;
            if (!reg.test(str)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断输入的字符是否为中文 cchar或者null,空
    function isChineseOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        if (obj.length != 0) {
            reg = /^[\u0391-\uFFE5]+$/;
            if (!reg.test(str)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断输入的邮编(只能为六位)是否正确 zip
    function isZip(obj) {
        if (obj.length != 0) {
            reg = /^\d{6}$/;
            if (!reg.test(str)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断输入的邮编(只能为六位)是否正确 zip或者null,空
    function isZipOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        if (obj.length != 0) {
            reg = /^\d{6}$/;
            if (!reg.test(str)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断输入的字符是否为双精度 double
    function isDouble(obj) {
        if (obj.length != 0) {
            reg = /^[-\+]?\d+(\.\d+)?$/;
            if (!reg.test(obj)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断输入的字符是否为双精度 double或者null,空
    function isDoubleOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        if (obj.length != 0) {
            reg = /^[-\+]?\d+(\.\d+)?$/;
            if (!reg.test(obj)) {
                return false;
            }
            else {
                return true;
            }
        }
    }
    //判断是否为身份证 idcard
    function isIDCard(obj) {
        if (obj.length != 0) {
            reg = /^\d{15}(\d{2}[A-Za-z0-9;])?$/;
            if (!reg.test(obj))
                return false;
            else
                return true;
        }
    }
    //判断是否为身份证 idcard或者null,空
    function isIDCardOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        if (obj.length != 0) {
            reg = /^\d{15}(\d{2}[A-Za-z0-9;])?$/;
            if (!reg.test(obj))
                return false;
            else
                return true;
        }
    }
    //判断是否为IP地址格式
    function isIP(obj) {
        var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g
//匹配IP地址的正则表达式
        if (re.test(obj)) {
            if (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3
< 256 && RegExp.$4 < 256) return true;
        }
        return false;
    }
    //判断是否为IP地址格式 或者null,空
    function isIPOrNull(obj) {
        var controlObj = $.trim(obj);
        if (controlObj.length == 0 || controlObj == null || controlObj
== undefined) {
            return true;
        }
        var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g
//匹配IP地址的正则表达式
        if (re.test(obj)) {
            if (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3
< 256 && RegExp.$4 < 256) return true;
        }
        return false;
    }
}
//提示信息
function ValidationMessage(obj, Validatemsg) {
    try {
        removeMessage(obj);
        obj.focus();
        var $poptip_error = $(‘<div class=”poptip”><span
class=”poptip-arrow
poptip-arrow-top”><em>◆</em></span>’ + Validatemsg

  • ‘</div>’).css(“left”, obj.offset().left + ‘px’).css(“top”,
    obj.offset().top + obj.parent().height() + 5 + ‘px’)
            $(‘body’).append($poptip_error);
            if (obj.hasClass(‘form-control’) || obj.hasClass(‘ui-select’))
    {
                obj.parent().addClass(‘has-error’);
            }
            if (obj.hasClass(‘ui-select’)) {
                $(‘.input-error’).remove();
            }
            obj.change(function () {
                if (obj.val()) {
                    removeMessage(obj);
                }
            });
            if (obj.hasClass(‘ui-select’)) {
                $(document).click(function (e) {
                    if (obj.attr(‘data-value’)) {
                        removeMessage(obj);
                    }
                    e.stopPropagation();
                });
            }
            return false; 
        } catch (e) {
            alert(e)
        }
    }
    //移除提示
    function removeMessage(obj) {
        obj.parent().removeClass(‘has-error’);
        $(‘.poptip’).remove();
        $(‘.input-error’).remove();
    }

//////////////////////////////分割线//////////////////////////////////

//如何使用

<script>
    function aSubmit() {
        if (!$(‘#form’).Validform()) {
            return false;
        }
    }
</script>
<div onclick=”aSubmit()”>提交验证</div>
<div id=”form”>
   <table>
       <tr>
           <th>用户名:</th>
           <td>
               <input id=”uname” type=”text” isvalid=”yes”
checkexpession=”NotNull” />
           </td>
       </tr>
       <tr>
           <th>余额:</th>
           <td>
               <input id=”uname” type=”text” isvalid=”yes”
checkexpession=”Num” />
           </td>
       </tr>
   </table>
</div>
//注解:(isvalid=”yes”  是否开启验证)(checkexpession=”Num” 
必须为数字)
//  Validform()     引用验证

 

/**
数据验证完整性 **/ $.fn.Validform = function () { var Validatemsg =
“”; var Validateflag = true; $(this).find(“[isvalid=yes…