JQuery插件第二十九个:公司的表单验证


(function($) {
    jQuery.fn.groupBsave = function() {
        var errcnt = 0;
        this.each(function() {
            var $err = $(this).next();
            $err.beforeSave({ type: -1 });
            if ($err.attr("ispass") == '1') {
                errcnt += $err.beforeSave({ type: -2, testValue: $(this).val() });
            }
            else if ($(this).val().length <= 0 || $(this).val() == '-1') {
                errcnt += $err.beforeSave({ type: 1 }); $(this).focus();
            }
            else {
                errcnt += $err.beforeSave({ testValue: $(this).val() });
            }
        });
        return errcnt;
    }
})(jQuery);
(function($) {
    jQuery.fn.beforeSave = function(options) {
        var defaults = {
            type: 0, 	           //正确 1错误 -1 清空
            error: "* 必填",  //错误语句
            testValue: ""          //待测值
        }
        var options = $.extend(defaults, options);
        var $obj = this;
        var rtn = 0;
        switch (options.type) {
            case -2:
                if (typeof $obj.attr("vtype") != 'undefined') {
                    var vtype = $obj.attr("vtype");
                    if (options.testValue.length > 0) {
                        //alert(vtype);
                        var vflag = false;
                        switch (vtype) {
                            case "int": vflag = isInteger(options.testValue, $obj);
                                break;
                            case "email": vflag = isEmail(options.testValue, $obj);
                                break;
                            case "str": vflag = isString(options.testValue, $obj);
                                break;
                            case "tel": vflag = isTelephone(options.testValue, $obj);
                                break;
                            case "mob": vflag = isMobile(options.testValue, $obj);
                                break;
                            case "url": vflag = isUri(options.testValue, $obj);
                                break;
                            case "post": vflag = isPost(options.testValue, $obj);
                                break;
                        }
                        if (!vflag) { rtn = 1; }
                    }
                }

                break;
            case -1:
                $obj.css("color", "black");
                $obj.html("");
                break;
            case 0:
                $obj.css("color", "black");
                $obj.html("√");
                if (typeof $obj.attr("vtype") != 'undefined') {
                    var vtype = $obj.attr("vtype");
                    var vflag = false;
                    switch (vtype) {
                        case "int": vflag = isInteger(options.testValue, $obj);
                            break;
                        case "email": vflag = isEmail(options.testValue, $obj);
                            break;
                        case "str": vflag = isString(options.testValue, $obj);
                            break;
                        case "tel": vflag = isTelephone(options.testValue, $obj);
                            break;
                        case "mob": vflag = isMobile(options.testValue, $obj);
                            break;
                        case "url": vflag = isUri(options.testValue, $obj);
                            break;
                        case "post": vflag = isPost(options.testValue, $obj);
                            break;
                    }
                    if (!vflag) { rtn = 1; }
                }

                break;
            case 1:
                $obj.css("color", "red");
                if ($obj.data("error") == null || $obj.data("error") == "") {
                    $obj.html(options.error);
                } else {
                    $obj.html($obj.data("error"));
                }
                rtn = 1;
                break;
        }
        return rtn;
    }
})(jQuery);


function isInteger(obj, errobj) {
    reg = /^[-+]?\d+$/;
    var flag = false;
    if (!reg.test(obj)) {
        errobj.css("color", "red");
        errobj.html("Please input correct figures");
    } else {
        errobj.css("color", "black");
        errobj.html("√");
        flag = true;
    }
    return flag;
}

function isEmail(obj, errobj) {
    reg = /^\w{3,}@\w+(\.\w+)+$/;
    var flag = false;
    if (!reg.test(obj)) {
        errobj.css("color", "red");
        errobj.html("请输入正确的邮箱地址");
    } else {
        errobj.css("color", "black");
        errobj.html("√");
        flag = true;
    }
    return flag;
}
function isString(obj, errobj) {
    reg = /^[a-z,A-Z]+$/;
    var flag = false;
    if (!reg.test(obj)) {
        errobj.css("color", "red");
        errobj.html("只能输入字符");
    } else {
        errobj.css("color", "black");
        errobj.html("√");
        flag = true;
    }
    return flag;
}
function isTelephone(obj, errobj) {
    reg = /^(\d{3,4}\-)?[1-9]\d{6,7}$/;
    var flag = false;
    if (!reg.test(obj)) {
        errobj.css("color", "red");
        errobj.html("例:021-1000001");
    } else {
        errobj.css("color", "black");
        errobj.html("√");
        flag = true;
    }
    return flag;
}

function isMobile(obj, errobj) {
    reg = /^(\+\d{2,3}\-)?\d{11}$/;
    var flag = false;
    if (!reg.test(obj)) {
        errobj.css("color", "red");
        errobj.html("请输入正确移动电话");
    } else {
        errobj.css("color", "black");
        errobj.html("√");
        flag = true;
    }
    return flag;
}
function isUri(obj, errobj) {
    reg = /^http:\/\/[a-zA-Z0-9]+\.[a-zA-Z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
    var flag = false;
    if (!reg.test(obj)) {
        errobj.css("color", "red");
        errobj.html("请输入正确的inernet地址");
    } else {
        errobj.css("color", "black");
        errobj.html("√");
        flag = true;
    }
    return flag;
}

function isPost(obj, errobj) {
    reg = /^[1-9]\d{5}$/;
    var flag = false;
    if (!reg.test(obj)) {
        errobj.css("color", "red");
        errobj.html("请输入正确的邮编");
    } else {
        errobj.css("color", "black");
        errobj.html("√");
        flag = true;
    }
    return flag;
}

$(function() {
    var vtypespan = $("span[vtype]");
    vtypespan.each(function() {
        if (typeof $(this).attr("vtype") != 'undefined') {
            var vtype = $(this).attr("vtype");
            var vobj = $(this);
            var input = $(this).prev();
            switch (vtype) {
                case "int":
                    input.keyup(function() {
                        return isInteger(input.val(), vobj);
                    });
                    break;
                case "email":
                    input.keyup(function() {
                        return isEmail(input.val(), vobj);
                    });
                    break;
                case "str":
                    input.keyup(function() {
                        return isString(input.val(), vobj);
                    });
                    break;
                case "tel":
                    input.keyup(function() {
                        return isTelephone(input.val(), vobj);
                    });
                    break;
                case "mob":
                    input.keyup(function() {
                        return isMobile(input.val(), vobj);
                    });
                    break;
                case "url":
                    input.keyup(function() {
                        return isUri(input.val(), vobj);
                    });
                    break;
                case "post":
                    input.keyup(function() {
                        return isPost(input.val(), vobj);
                    });
                    break;
            }
        }
    });
})



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <link rel="stylesheet" type="text/css" href="../css/index.css" />
    <script src="../js/jquery-1.4.2.js" type="text/javascript"></script>
    <script src="../js/jQuery.groupBsave.js" type="text/javascript"></script>
    <script type="text/javascript">
        function funcSave() {
            $("#dSortid").data("error", "请填写排序ID");
            $("#dClassName").data("error", "请填写类别名称");
            var sign = $(".a09").groupBsave();
            if (sign > 0) {
                return false;
            }
        }

        $(function() {
            $("#BtnAdd").click(function() {
                return funcSave();
            })
        });
    </script>
    
</head>
<body class="bodybg01">
    <form name="form1" method="post"  id="form1">

    <input type="hidden" name="ClasSN" id="ClasSN" />
    <div id="content">
        <table width="100%" class="a01">
            <tr>
                <td width="8%" style="text-align: right;">
                    当前位置:
                </td>
                <td width="3%">
                    首页
                </td>
                <td width="1%">
                    >
                </td>
                <td width="5%">
                    <a href="#">客户备案</a>
                </td>
                <td width="1%">
                    >
                </td>
                <td>
                    客户分类管理
                </td>
            </tr>
        </table>
        <table width="100%" class="a02">
            <tr>
                <td width="5%" style="text-align: right;">
                    <img src="../images/tubiao.jpg" alt=" " />
                </td>
                <td width="1%">
                     
                </td>
                <td width="94%">
                    <strong id="StrongTitle">客户分类管理新增</strong>
                </td>
            </tr>
        </table>
        <div class="shdlr">
            <table width="100%" class="a03">
                <tr>
                    <td width="10%" style="text-align: right;">
                        排序ID:
                    </td>
                    <td width="23%">
                        <input name="Sortid" type="text" id="Sortid" class="a09" />
                        <span id="dSortid" ></span>
                    </td>
                    <td style="text-align: right;">
                         
                    </td>
                </tr>
                <tr>
                    <td style="text-align: right;">
                        类别名称:
                    </td>
                    <td>
                        <input name="ClassName" type="text" id="ClassName" class="a09" />
                        <span id="dClassName" ></span>
                    </td>
                    <td style="text-align: right;" >
                         
                    </td>
                </tr>
            </table>
            <table width="100%" style="margin-bottom: 20px;">
                <tr>
                    <td width="10%">
                         
                    </td>
                    <td width="10%">
                            <a id="BtnAdd" href="javascript:void(0);"><img src="../images/querentijiao.jpg" /></a>
                    </td>
                    <td width="10%">
                        <a href="javascript:history.go(-1);" >
                            <img src="../images/quxiaofanhui.jpg" alt=" " /></a>
                    </td>
                    <td width="70%">
                         
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html>

 

你可能感兴趣的:(JavaScript,function,Class,jquery插件,input,stylesheet)