Fix min/max validation. Closes gh-666. Fixes #648

In 1.10.0, min/max validation was supported for input type="text",
where min/max were interpreted as numbers.  This means min/max
for date would not work: min="2012-02-13" was interpreted as min="Not a Number".

In 1.11.0, min/max were no longer converted to numbers.  This means
min/max for dates worked, but min/max for numbers failed:
"50" < "150" < "1000" does not hold.

For an example, see http://jsbin.com/awokex/3

This commit makes the behaviour of min/max dependent on input type:

 * input type=text (or not type attribute) has numeric min/max, as in 1.10.0
 * input type=date has working min/max for type date;
   on mobile browsers you also get a date picker,
   plus the browser may reject invalid dates before
   javascript gets a chance to complain.
 * input type=number or range get numeric min/max,
   plus numeric keypad or slider on mobile browsers,
   plus browser may reject invalid input before javascript
   gets a chance to complain

Allowing use of min/max with type=number/range/date is important
for mobile browsers, where the numeric keypad or date picker
make the input much easier to use than a generic text input field.
In this situation jquery-validate remains necessary to support
older browsers that do not do input validation based on type
and min/max.

For situations where numeric input should be validated by jquery
without giving the browser a chance to validate the input format,
input type=text in combination with min/max can be used, as in 1.10.0.
This commit is contained in:
Erik van Konijnenburg
2013-02-23 17:47:21 +01:00
committed by Jörn Zaefferer
parent 4a134b6db6
commit 5b114e10db
4 changed files with 148 additions and 18 deletions

11
jquery.validate.js vendored
View File

@@ -840,6 +840,7 @@ $.extend($.validator, {
attributeRules: function( element ) {
var rules = {};
var $element = $(element);
var type = $element[0].getAttribute("type");
for (var method in $.validator.methods) {
var value;
@@ -858,9 +859,17 @@ $.extend($.validator, {
value = $element.attr(method);
}
// convert the value to a number for number inputs, and for text for backwards compability
// allows type="date" and others to be compared as strings
if ( /min|max/.test( method ) && ( type === null || /number|range|text/.test( type ) ) ) {
value = Number(value);
}
if ( value ) {
rules[method] = value;
} else if ( $element[0].getAttribute("type") === method ) {
} else if ( type === method && type !== 'range' ) {
// exception: the jquery validate 'range' method
// does not test for the html5 'range' type
rules[method] = true;
}
}