Simple datepicker validation

后端 未结 3 994
误落风尘
误落风尘 2020-12-22 06:06

Can someone help me write a function for showing a line of text (preferably by showing a css class) for a jquery datepicker after someone has clicked a date?

The not

相关标签:
3条回答
  • 2020-12-22 06:43

    just use onSelect property when you init datepicker

    $( "#delivery_date" ).datepicker({ 
        dateFormat: "dd/mm/yy",
        minDate : new Date(),
        onSelect: function(str) {
    
            var lastDate = new Date(),
                nums = str.split("/"),
                userDate = new Date(nums[2], nums[1]-1, nums[0]), // reverse date
                period = 21;
    
            lastDate.setDate(lastDate.getDate()+period);
    
            if (userDate<lastDate) {
                // notify
                alert("inside 21 days period");
            }
        }
    });
    

    try FIDDLE

    0 讨论(0)
  • 2020-12-22 06:54

    You might be able to validate it using date.js, it has a fluent API which makes it very easy to express your conditions.

    Example code might be:

    var enteredDate = $("#delivery_date").prop("value");
    var parsed = Date.parse(enteredDate); 
    var start = Date.today();
    var end = Date.today().add(21).days(); 
    if (!parsed.between(start, end)) {
        alert("Date is valid");
    } else {
        alert("Date is not valid");
    }
    

    Test plunker

    0 讨论(0)
  • 2020-12-22 07:04

    U can use onClose in datepicker

    $(function() {
      $( "#date" ).datepicker({ 
      dateFormat: "dd/mm/yy",
      onClose: function() {
            valid();
        }
      });
    });
    

    jsfiddle - example

    0 讨论(0)
提交回复
热议问题