mirror of
https://github.com/jquery-validation/jquery-validation.git
synced 2025-12-20 09:11:40 +01:00
Core: Update elementValue method to deal with type="number" fields
The HTML5 draft defines, for type="number" inputs: "The value sanitization algorithm is as follows: If the value of the element is not a valid floating-point number, then set it to the empty string instead." If the constraint validation considers the input invalid, return false as the value, instead of the empty string, for the number and digit methods to output their messages. This would break in browsers that support type="number", but not the constraint validation API. I don't know of any existing browser where that applies. Fixes #858 Fixes #922 Closes #1093
This commit is contained in:
committed by
Jörn Zaefferer
parent
104e8efceb
commit
d09a5ee199
@@ -554,6 +554,8 @@ $.extend($.validator, {
|
||||
|
||||
if ( type === "radio" || type === "checkbox" ) {
|
||||
return $("input[name='" + element.name + "']:checked").val();
|
||||
} else if ( type === "number" && typeof element.validity !== "undefined" ) {
|
||||
return element.validity.badInput ? false : $(element).val();
|
||||
}
|
||||
|
||||
val = $element.val();
|
||||
|
||||
48
test/number.html
Normal file
48
test/number.html
Normal file
@@ -0,0 +1,48 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Test for jQuery validate() plugin</title>
|
||||
|
||||
<script src="jquery.js" type="text/javascript"></script>
|
||||
<script src="../dist/jquery.validate.js"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
$('#numberForm').validate({
|
||||
submitHandler: function() {
|
||||
alert( "All valid" );
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<style type="text/css">
|
||||
#numberForm { width: 500px; }
|
||||
#numberForm label { width: 250px; display: block; float: left; }
|
||||
#numberForm label.error, #commentForm input.submit { margin-left: 253px; }
|
||||
#numberForm label.error { background-color: red; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<form id="numberForm" method="get" action="">
|
||||
<fieldset>
|
||||
<legend>Testing type="number"</legend>
|
||||
<p>
|
||||
<label for="Digits">Number with digits</label>
|
||||
<input id="NumberDigits" name="NumberDigits" class="digits" type="number" required>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<label for="Digits">Number with number</label>
|
||||
<input id="NumberNumber" name="NumberNumber" class="number" type="number" required>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<input class="submit" type="submit" value="Submit">
|
||||
</p>
|
||||
</fieldset>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user