分享

private

 ZGJson 2012-02-29
$(document).ready(function () {
    //注册函数
    $.extend({
        validateForm: function (form, ary) {
            $('#' + form).bind("submit", function () {
                var result = true;
                $.each(ary.rules, function (key, value) {
                    var input = $('#' + key);
                    if (!validateInput(input, value, ary.msg[key])) {
                        result = false;
                    }
                });
                return result;
            });
            $.each(ary.rules, function (key, value) {
                $('#' + key).bind("blur", function () { validateInput($(this), value, ary.msg[key]) });
            });
        },
        clickValidate: function (btn, ary, method) {
            $('#' + btn).bind("click", function () {
                var result = true;
                $.each(ary.rules, function (key, value) {
                    var input = $('#' + key);
                    if (!validateInput(input, value, ary.msg[key])) {
                        result = false;
                    }
                });
                if (result) {
                    if (method) {
                        method();
                    }
                }
                return result;
            });
            $.each(ary.rules, function (key, value) {
                $('#' + key).bind("blur", function () { validateInput($(this), value, ary.msg[key]) });
            });
        }
    });

    //input 验证函数
    function validateInput(input, option, msg) {
        var input = $(input);
        if (option) {
            //空值验证
            if (option.required) {
                if (!input.val() || input.val() == "" || input.val() == null) {
                    errorPlacement(msg.required, input);
                    return false;
                }
            }
            //长度验证
            if (option.rangelength) {
                if (input.val().length < option.rangelength[0] || input.val().length > option.rangelength[1]) {
                    errorPlacement(msg.rangelength, input);
                    return false;
                }
            }
            //比较对象值
            if (option.equalsTo) {
                if (input.val() != $('#' + option.equalsTo).val()) {
                    errorPlacement(msg.equalsTo, input);
                    return false;
                }
            }
            //是否为数字
            if (option.number) {
                if (isNaN(input.val())) {
                    errorPlacement(msg.number, input);
                    return false;
                }
            }
            //最小值验证
            if (option.min) {
                if (isNaN(input.val())) {
                    errorPlacement(msg.min, input);
                    return false;
                } else {
                    if (parseInt(input.val()) < parseInt(option.min)) {
                        errorPlacement(msg.min, input);
                        return false;
                    }
                }
            }
            //最大值验证
            if (option.max) {
                if (isNaN(input.val())) {
                    errorPlacement(msg.max, input);
                    return false;
                } else {
                    if (parseInt(input.val()) > parseInt(option.max)) {
                        errorPlacement(msg.max, input);
                        return false;
                    }
                }
            }
            //正则表达式验证
            if (option.regex) {
                if (input.val() != "") {
                    var regex = new RegExp(option.regex);
                    if (!regex.test(input.val())) {
                        errorPlacement(msg.regex, input);
                        return false;
                    }
                }
            }
            //自定义函数
            if (option.method) {
                var bool = option.method();
                if (!bool) {
                    errorPlacement(msg.method, input);
                    return false;
                }
            }
            successPlacement(input);
        }
        return true;
    }

    function errorPlacement(msg, element) {
        $(element).parent().find("span.input_tip").each(function () {
            $(this).remove();
        });
        $('<span class="input_tip"><span class="left"></span><span class="center">' + msg + '</span><span class="right"></span></span>').appendTo($(element).parent());
    }
    function successPlacement(element) {
        $(element).parent().find("span.input_tip").each(function () {
            $(this).remove();
        });
    }




});

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约