Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your question is something I've been trying to figure out for a while too. This is what I have so far.</p> <p>I use this for validating a single date range. It takes input fields as params rather than dates:</p> <pre><code>jQuery.validator.addMethod("rangeDate", function(value, element, params) { try { var beforedate=$.datepicker.parseDate($(params[0]).datepicker('option','dateFormat'),$(params[0]).val()); var afterdate=$.datepicker.parseDate($(params[1]).datepicker('option','dateFormat'),$(params[1]).val()); var qdate=$.datepicker.parseDate($(element).datepicker('option','dateFormat'),value); return this.optional(element) || (qdate &gt;= beforedate &amp;&amp; qdate&lt;=afterdate); } catch(err){ return false; } }, function(params){ return "Date must occur between " + $(params[0]).val() + ' and ' + $(params[1]).val(); } ); </code></pre> <p>and the rule looks like this:</p> <pre><code>rules:{ between_date: { dateCan: true, rangeDate: { depends: function(element) { //check that params exist return $("input[name='before_date']").valid() &amp;&amp; $("input[name='after_date']").valid(); }, param: ["input[name='before_date']", "input[name='after_date']"] } } } </code></pre> <p>After experimenting a bit, I've seen that it's possible to have more than one <code>rangeDate</code> rule for a field. However, the last call appears to overwrite the results of all the previous <code>rangeDate</code> calls for that field. So I <em>think</em> what might work is to add another custom rule that accepts an array of pairs of fields which can be fed to <code>rangeDate</code>. The problem with this approach is that the the <code>depends</code> clause for the <code>rangeDate</code> rule becomes unwieldy as you get more date pairs. This is especially so if you only require at least one pair to be present rather than all pairs.</p> <p>Of course, if the date ranges aren't dynamic, the problem is easier because no <code>depends</code> clause is necessary. Just rewrite <code>rangeDate</code> to accept dates rather than fields and write a wrapper rule to accept an array of pairs of dates.</p> <p>If you've found a solution to the problem, I'd love to see it. Hopefully it will be more elegant than mine.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload