【问题标题】:Can I simplify/optimise this Jquery code at all?我可以简化/优化这个 Jquery 代码吗?
【发布时间】:2009-08-25 15:19:48
【问题描述】:

我编写了一些代码来检查两个日期 - 它们分为两天输入(#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! ^

}

对不起,如果我似乎在问一个愚蠢的问题,因为我的代码工作正常,我只是认为这是一种垃圾方式,有很多重复,但我真的想不出任何方式效率更高?

谢谢

【问题讨论】:

    标签: jquery validation optimization


    【解决方案1】:
    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;
        }
    }
    

    当然,对表单中的所有错误提供反馈而不仅仅是第一个错误的验证有点,你知道,更好。

    【讨论】:

      【解决方案2】:

      乍一看,您可以从这些相同的部分创建一个函数,然后根据需要调用它。 You shouldn't repeat yourself.

      这是一个blog post,关于验证日期可能很有启发性。这是一个 SO 答案,它提供了一个jQuery plugin answer 迄今为止的验证。

      【讨论】:

      • 在这种情况下 SO 是什么意思?我今天之前见过!
      • 堆栈溢出。您所在的网站。
      【解决方案3】:

      是的,在这里:

      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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多