2017-08-30 81 views
0

我學會了IE瀏覽器在使用jQuery Validate Plugin(https://jqueryvalidation.org/)驗證日期時遇到的一些問題。jQuery .validate - 在Internet Explorer中驗證日期

我的規則只是starttime: {required: true, date: true},但我的格式是DD-MMM-YYYY hh:mm a,即2017年8月31日上午9:43。

這適用於其他瀏覽器,但IE說它是無效的日期。請注意,驗證正在處理其他領域。

這裏有一些很好的答案:Custom date format with jQuery validation plugin但具體而言,我如何在IE中驗證我的日期格式?

我的猜測是我需要添加一個自定義的方法,但它應該是什麼? RegEx或自定義日期,以及如何?對於RegEx驗證日期的格式而不驗證日期本身(例如32/01/2017),我確定,因爲此頁面使用日期選擇器,並且用戶無法直接輸入數據。

EDIT(包括實際的代碼):

<script type="text/javascript" src="js/plugins/jquery-validate/jquery.validate.min.js"></script> 

<script> 
    $(document).ready(function(){ 

     if ($("#newinspectionform").length > 0) { 
      var validate = $("#newinspectionform").validate({ 
       errorClass: "has-error", 
       validClass: "", 
       errorElement: "span", 
       ignore: [], 
       errorPlacement: function (error, element) { 
        $(element).after(error); 
        $(element).parents(".form-group").addClass("has-error"); 
       }, 
       highlight: function (element, errorClass) { 
        $(element).parents(".form-group").removeClass("has-success").addClass(errorClass); 
       }, 
       unhighlight: function (element, errorClass, validClass) { 
        $(element).parents(".form-group").removeClass(errorClass).addClass(validClass); 
       }, 
       rules: { 
        propertyid: {required: true}, 
        type: {required: true}, 
        starttime: {required: true, date: true}, 
        endtime: {required: true, date: true} 
       } 
      }); 
     } 

    ... other stuff 
</script> 
+0

你檢查了這個? https://stackoverflow.com/questions/4809451/date-validator-on-datepicker-trigger-false-negatives-in-ie7-ie8 –

+0

是的 - 有一些這樣的,但不幸的是這些解決方案並沒有幫助這種情況。 – Warren

回答

0

我與自定義的日期方法解決了這個問題。我需要這樣做,因爲我的格式需要保持爲DD-MMM-YYYY hh:mm a,即2017年8月31日上午9:43。

$.validator.addMethod(
     "inspDateTime", 
     function(value, element) { 
      // put your own logic here, this is just a (crappy) example 
      return value.match(/^(0?[1-9]|[12][0-9]|3[0-1])[/., -](0?[1-9]|1[0-2]|[a-zA-Z]{3})[/., -](19|20)?\d{2} (0?[1-9][012]?:[0-5][0-9] [ap]m)$/); 
     }, 
     "Please enter a valid date." 
    ); 

我不確定爲什麼Internet Explorer無法驗證這個日期,但上面的正則表達式在IE中工作。信用這個答案爲起始正則表達式:https://stackoverflow.com/a/17392795/2066625

0

編輯:包括日期選擇器

嘗試。它在IE中使用datePicker。你是否也想設定時間?如果是這樣,我知道有些人已經爲datetimePicker創建了一個插件。

<!doctype html> 
<html> 
<head> 
</head> 
<body> 
    <form> 
     <input type="date" class="left" id="starttime" name="starttime"> 
     <br/> 
     <input type="submit" value="Validate!"> 
    </form> 
</body> 
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script> 
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script> 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
<script> 
    //datePicker Set format 
    $('#starttime').datepicker({ 
     dateFormat: 'yy-mm-dd' 
    }).datepicker("setDate", new Date()); 


    var validator = $("form").validate ({ 
     rules: { 
      starttime: {required: true, date: true} 
     }, 
     messages: { 
      starttime: "Please enter a valid start date" 
     } 
    }); 
</script> 

+0

我剛剛編輯我的問題以包含代碼。正如你將會看到的那樣,它被封裝在'$(document).ready()'中。另外,其他驗證工作 - 這只是問題的日期 – Warren