开发者

Validating broken up date width jQuery Validation plugin

jQuery Validation plugin is used to validate all form data: http://docs.jquery.com/Plugins/Validation

Have 3 select fields for birth date: day, month, year.

First: How i can make sure that user selects all 3 fields and 'invalid' icon is displayed only when one of all three fields is not selected. For example, now i have those 3 select fields, but when i select first and it's ok, then plugin displays 'ok' icon even if other two select fields (month, year) are not selected.

Second: How i can validate those 3 select fields and make sure that the person, which birth date is selected in those 3 select fields, is older than 18 years?

<select name="DOBd" id="DOBd">
    <option value="">DD</option>
    // day options
</select>
<select name="DOBm" id="DOBm">
    <option value="">MM</option>
    // month options
</select>
<select name="DOBy" id="DOBy">
    <option value="">YYYY</option>
    // year options
</select>

Al开发者_JS百科so i set up that 'error' icon is displayed not after each one of them, but after all of them:

groups:{
    date_of_birth:"DOBd DOBm DOBy"
},
errorPlacement:function(error,element){
    if (element.attr("name") == "DOBd" || element.attr("name") == "DOBm" || element.attr("name") == "DOBy")
        error.appendTo(element.parent("td").next("td"));
    else
        error.appendTo(element.parent("td").next("td"));
},


You can add custom methods using $.validator.addMethod

Add two methods: one for checking for all 3 selections (FullDate) and one for checking age (Age). Since the 3 elements are grouped, I just put one method on one selector and the other on another selector.

Also, your errorPlacement function has an if/else that does the exact same thing, which isn't necessary.

$(function() {
  // this function requires month day and year selections
  $.validator.addMethod("FullDate", function() {
    //if all values are selected
    if($("#DOBd").val() != "" && $("#DOBm").val() != "" && $("#DOBy").val() != "") {
      return true;
    } else {
      return false;
    }
  }, '<img src="/path/to/image.png" alt="Please select a day, month, and year" title="Please select a day, month, and year" />');

  // this function checks the selected date, returning true if older than 18 years
  $.validator.addMethod("Age", function() {
    //only compare date if all items have a value
    if($("#DOBd").val() != "" && $("#DOBm").val() != "" && $("#DOBy").val() != "") {
      //get selected date
      var DOB = new Date($("#DOBy").val(), $("#DOBm").val(), $("#DOBd").val());
      var eday = new Date(); //get today
      eday.setFullYear(eday.getFullYear() - 18); //set to eighteen years ago
      //if older than 18
      if(DOB < eday) {
        return true;
      } else {
        return false;
      }
    }
    return true;
  }, '<img src="/path/to/image.png" alt="Must be 18" title="Must be 18" />');

  $("form").validate({
    rules: {
      DOBd: "FullDate",
      DOBm: "Age"
    },
    groups:{
      date_of_birth:"DOBd DOBm DOBy"
    },
    errorPlacement:function(error,element){
      error.appendTo(element.parent("td").next("td"));
    }
  });    
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜