ASP.NET MVC4 validate驗證遇到DateTime 類型的Bug(完整解決方案)

來源:互聯網
上載者:User

近期在使用MVC4開發項目的時候,突然遇到一個比較奇特的bug,就是在用mvc4 內建的validate.js驗證DateTime的時候總是報錯(如)。

 

 經過反覆檢測,我發現以下幾個問題:

1、如果日期格式是“2012/10/22”的時候,驗證就通過了。

2、IE6,7,8等瀏覽器才會出現驗證錯誤,IE9和FireFox(最新)並未出現類似bug.

 

綜上所述,我上網查了下資料最後看到這樣的一個文章: demo.tc/Post/762

簡單的來說,這是一個bug.

具體的解決方案如下:

建立一個js檔案:jquery.validate.plus.js

jQuery.validator.methods.date = function (value, element) {
  return this.optional(element) || (/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(value));}

 

然後在引用js庫的時候這樣處理就行了:

<script type="text/javascript" src="/Scripts/jquery.validate.js"></script>
<script type="text/javascript" src="/Scripts/jquery.validate.plus.js"></script> 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.