2012-06-13 60 views
0

我需要通過使用jQuery驗證程序與三個單獨的文本框日日月和年進行驗證。如何驗證出生日期與jQuery驗證程序的日期月年的三個文本框

如何做到這一點,請幫助。

HTML代碼

<input type="text" maxlength="2" placeholder="DD" class="dob-day fillone" name="dob-day" id="dob-day" data-fieldgroup="dob" data-fillone-field="true" /> 
<input type="text" maxlength="2" placeholder="MM" id="dob-month" class="dob-month fillone" data-fieldgroup="dob" data-fillone-field="true"> 
<input type="text" maxlength="4" placeholder="YYYY" class="dob-year fillone" id="dob-year" data-fieldgroup="dob" data-fillone-field="true"> 

我使用的數據組進行分組文本框

回答

0

對不起,我不會說英語

,您可以檢查日期< 30或31分別月份 月年...

我覺得這很容易,當你使用d三個TextBox :( 只是js的

3

您應該創建一個自定義的驗證方法,這與使用groups選項,驗證提供了沿:

/* Custom validation method to validate a date based on several fields: */ 
$.validator.addMethod("datemultiple", function(value, element, params) { 
    var daySelector = params[0], 
     monthSelector = params[1], 
     yearSelector = params[2], 
     day = parseInt($(daySelector).val(), 10), 
     month = parseInt($(monthSelector).val(), 10), 
     year = parseInt($(yearSelector).val(), 10), 
     dateEntered = new Date(year, month - 1, day); 

    return this.optional(element) || !isNaN(dateEntered.valueOf()); 

}, "Please enter a valid date"); 

$(document).ready(function() { 
    $("#myform").validate({ 
     groups: { 
      /* Only display one validation message for day, month, and year: */ 
      dateOfBirth: "dob-day dob-month dob-year" 
     }, 
     rules: { 
      'dob-day': { 
       required: true, 
       datemultiple: ["#dob-day", "#dob-month", "#dob-year"] 
      }, 
      'dob-month': { 
       required: true 
      } 
     }, 
     /* Place error messages after the "year" field */ 
     errorPlacement: function ($error, $element) { 
      if ($element.data("fieldgroup") === "dob") { 
       $error.insertAfter("#dob-year"); 
      } 
     } 
    }); 
}); 

例子:http://jsfiddle.net/xHC86/

+0

如果你想 「等待」 的所有3個輸入什麼填寫並驗證?當然,你可以檢查它們是否全部填滿,然後驗證。但是當你實際提交時,這會造成問題 - 按下提交按鈕。 – Eugene

+0

@Eugene:我不太清楚你的意思...... –

+0

在你的例子中,如果我填寫一個字段,然後移動到第二個 - 顯示錯誤消息。如果我想等待用戶填寫所有三個元素然後驗證?這就像跨領域驗證。 – Eugene

2

我寫的一個處理數據是否有效的Javascript模塊,你可以在JSFiddle鏈接中查看完整的工作示例。

http://jsfiddle.net/dceast/vmHjN/

下面是確實的驗證模塊:

var compareDate, checkDates = false; 
var validateObject = { 
    init: function(year, month, day) { 
     return this.compareDate.init(year, month, day); 
    }, 
    compareDate: { 
     init: function(year, month, day) { 
      var isValid = false; 
      // Compensate for zero based index, if month was not 
      // subtracted from one 0 === Jan, 1 === Feb, 2 === Mar 
      month -= 1; 

      // Create a new date object with the selected 
      // year, month, and day values and retrieve the 
      // milliseconds from it. 
      var mSeconds = (new Date(year, month, day)).getTime(); 
      var objDate = new Date(); 

      // Set the time of the object to the milliseconds 
      // retrieved from the original date. This will 
      // convert it to a valid date. 
      objDate.setTime(mSeconds); 

      // Compare if the date has changed, if it has then 
      // the date is not valid 
      if (objDate.getFullYear() === year && 
       objDate.getMonth() === month && 
       objDate.getDate() === day) 
      { 
       isValid = true; 
      } 
      return isValid; 
     } 
    } 
}; 
+0

它是一個很好的代碼,但它會很好,如果你可以請在jquery validator formate中更改這個 – user617204

0
var compareDate, checkDates = false; 
var validateObject = { 
    init: function(year, month, day) { 
     return this.compareDate.init(year, month, day); 
    }, 
    compareDate: { 
     init: function(year, month, day) { 
      var isValid = false; 
      // Compensate for zero based index, if month was not 
      // subtracted from one 0 === Jan, 1 === Feb, 2 === Mar 
      month -= 1; 

      // Create a new date object with the selected 
      // year, month, and day values and retrieve the 
      // milliseconds from it. 
      var mSeconds = (new Date(year, month, day)).getTime(); 
      var objDate = new Date(); 

      // Set the time of the object to the milliseconds 
      // retrieved from the original date. This will 
      // convert it to a valid date. 
      objDate.setTime(mSeconds); 

      // Compare if the date has changed, if it has then 
      // the date is not valid 
      if (objDate.getFullYear() === year&& 
       objDate.getMonth() === month && 
       objDate.getDate() === day) 
      {     
       if(objDate <= new Date()) 
       { 
        isValid = true; 
       }     
      } 
      return isValid; 
     } 
    } 
}; 

$(function() { 
    var validateButton = $('#btValidate'); 

    validateButton.click(function(e) { 
     var month = parseInt(document.getElementById('month').value, 0), 
      day = parseInt(document.getElementById('day').value, 0), 
      year = parseInt(document.getElementById('year').value, 0), 
      alertBox = $('#alert'), 
      isValid = false; 

     isValid = validateObject.init(year, month, day); 
     var color, message; 

     if (isValid === true) 
     { 
      color = "#008000"; 
      message = "Your date is valid!"; 
     } 
     else if (isValid === false) 
     { 
      color = "#F00"; 
      message = "Your date is not valid!"; 
     } 
     alertBox.css('background', "" + color) 
      .html("<p>"+ message +"</p>") 
      .stop() 
      .animate({ 
       width: "200px", 
       paddingLeft: "75px" 
      }, 1750, "easeOutBounce", function() { 
       $(this).animate({ 
        width: "0px", 
        paddingLeft: "0px" 
       }, 1000, "easeInBounce"); 
     }); 
    }); 
}); 

工作代碼在這裏: http://jsfiddle.net/vmHjN/140/

+1

請將代碼直接粘貼到你的答案中,並且還提供一些關於代碼如何幫助OP的解釋。 – 2012-06-20 11:35:25