我可以简化/优化这个Jquery代码吗?

我可以简化/优化这个Jquery代码吗?,jquery,validation,optimization,Jquery,Validation,Optimization,我已经编写了一些代码来检查两个日期-它们被分为两天输入enddate-1-dd,date-1-dd,两个月输入enddate-1-mm,date-1-mm和两年输入enddate-1,date-1 首先我想检查它们是否都是数字,然后我想检查每一个以确保它是日期格式的,现在是这样的: function validate_form () { retVal = true; // if the statements below fail, return true if(retVal == true)

我已经编写了一些代码来检查两个日期-它们被分为两天输入enddate-1-dd,date-1-dd,两个月输入enddate-1-mm,date-1-mm和两年输入enddate-1,date-1

首先我想检查它们是否都是数字,然后我想检查每一个以确保它是日期格式的,现在是这样的:

function validate_form () {

retVal = true; // if the statements below fail, return true

if(retVal == true) {
    // check whether the available hours they've entered are a valid time!
    $(":text").each(function() {
        $this = $(this); // cache the object
        if (isNaN($this.val())) {
            $this.focus();
            $.jGrowl('Please enter a valid date!', { theme: 'smoke' });
            retVal = false; return false;
        }
    });
}

if(retVal == true) {
    $("#date-1-dd").each(function() {
        $this = $(this); // cache the object
        if ($this.val() > 31) {
            $this.focus();
            $.jGrowl('Please enter a valid day, should be no more than 31!', { theme: 'smoke' });
            retVal = false; return false;
        }
    });
}

if(retVal == true) {
    $("#enddate-1-dd").each(function() {
        $this = $(this); // cache the object
        if ($this.val() > 31) {
            $this.focus();
            $.jGrowl('Please enter a valid day, should be no more than 31!', { theme: 'smoke' });
            retVal = false; return false;
        }
    });
}

if(retVal == true) {
    $("#date-1-mm").each(function() {
        $this = $(this); // cache the object
        if ($this.val() > 12) {
            $this.focus();
            $.jGrowl('Please enter a valid month, should be no more than 12!', { theme: 'smoke' });
            retVal = false; return false;
        }
    });
}

if(retVal == true) {
    $("#enddate-1-mm").each(function() {
        $this = $(this); // cache the object
        if ($this.val() > 12) {
            $this.focus();
            $.jGrowl('Please enter a valid month, should be no more than 12!', { theme: 'smoke' });
            retVal = false; return false;
        }
    });
}

if(retVal == true) {
    $("#date-1").each(function() {
        $this = $(this); // cache the object
        if ($this.val() < 1900 || $this.val() > 3000) {
            $this.focus();
            $.jGrowl('Please enter a valid year!', { theme: 'smoke' });
            retVal = false; return false;
        }
    });
}

if(retVal == true) {
    $("#enddate-1").each(function() {
        $this = $(this); // cache the object
        if ($this.val() < 1900 || $this.val() > 3000) {
            $this.focus();
            $.jGrowl('Please enter a valid year!', { theme: 'smoke' });
            retVal = false; return false;
        }
    });
}

return retVal; // return either true or false, depending on what happened up there! ^
}

抱歉,如果我问了一个愚蠢的问题,因为我的代码运行正常,我只是认为这是一种垃圾的方式,重复很多,但我真的想不出任何方法可以更有效地执行它


谢谢

乍一看,您可以从这些相同的部分创建一个函数,并根据需要调用它

下面是一个关于验证日期的示例,可能会有所启发。这里的答案给出了一个迄今为止的验证

function validate_form_checks() {
    var error;
    // check whether the available hours they've entered are a valid time!
    $(':text').each(function() {
        if(isNaN($(this).val())) {
            $(this).focus();
            error = 'Please enter a valid date!';
            return false;
        }
    });
    if(error)
        return error;
    $('#date-1-dd, #enddate-1-dd').each(function() {
        if($(this).val() > 31) {
            $(this).focus();
            error = 'Please enter a valid day, should be no more than 31!';
            return false;
        }
    });
    if(error)
        return error;
    $('#date-1-mm, #enddate-1-mm').each(function() {
        if($(this).val() > 12) {
            $(this).focus();
            error = 'Please enter a valid month, should be no more than 12!';
            return false;
        }
    });
    if(error)
        return error;
    $('#date-1, #enddate-1').each(function() {
        if($(this).val() < 1900 || $(this).val() > 3000) {
            $(this).focus();
            error = 'Please enter a valid year!';
            return false;
        }
    });
    if(error)
        return error;
    return true;
}

function validate_form() {
    var result = validate_form_checks();
    if(result === true) {
        return true;
    } else {
        $.jGrowl(result, { theme: 'smoke' });
        return false;
    }
}
当然,验证可以提供表单中所有错误的反馈,而不仅仅是第一个错误,这是一种更好的方式。

是的,这里:

function validate_form() {
return retVal = !0, retVal == 1 && $(":text")
    .each(function () {
    return $this = $(this), isNaN($this.val()) ? ($this.focus(), $.jGrowl("Please enter a valid date!", {
        theme: "smoke"
    }), retVal = !1, !1) : void 0
}), retVal == 1 && $("#date-1-dd")
    .each(function () {
    return $this = $(this), $this.val() > 31 ? ($this.focus(), $.jGrowl("Please enter a valid day, should be no more than 31!", {
        theme: "smoke"
    }), retVal = !1, !1) : void 0
}), retVal == 1 && $("#enddate-1-dd")
    .each(function () {
    return $this = $(this), $this.val() > 31 ? ($this.focus(), $.jGrowl("Please enter a valid day, should be no more than 31!", {
        theme: "smoke"
    }), retVal = !1, !1) : void 0
}), retVal == 1 && $("#date-1-mm")
    .each(function () {
    return $this = $(this), $this.val() > 12 ? ($this.focus(), $.jGrowl("Please enter a valid month, should be no more than 12!", {
        theme: "smoke"
    }), retVal = !1, !1) : void 0
}), retVal == 1 && $("#enddate-1-mm")
    .each(function () {
    return $this = $(this), $this.val() > 12 ? ($this.focus(), $.jGrowl("Please enter a valid month, should be no more than 12!", {
        theme: "smoke"
    }), retVal = !1, !1) : void 0
}), retVal == 1 && $("#date-1")
    .each(function () {
    return $this = $(this), 1900 > $this.val() || $this.val() > 3e3 ? ($this.focus(), $.jGrowl("Please enter a valid year!", {
        theme: "smoke"
    }), retVal = !1, !1) : void 0
}), retVal == 1 && $("#enddate-1")
    .each(function () {
    return $this = $(this), 1900 > $this.val() || $this.val() > 3e3 ? ($this.focus(), $.jGrowl("Please enter a valid year!", {
        theme: "smoke"
    }), retVal = !1, !1) : void 0
}), retVal
}

在这种情况下,这意味着什么?我今天以前见过!堆栈溢出。你所在的网站。