I found that when jQuery get a value with 0 (for example 100)trail, it will omit it. So if I compare 5>100, the result is true. So how do I solve this?
here is the HTML code:
<form id="target">
<input type="text" id="max" value="100"/>
<input type="text" id="number" />
<input id="submit" type="submit" value="submit" />
</form>
And here is jquery:
$('#target').submit( function() {
var a = $("#number").val();
var b = $("#max").val();
if( a > b){
alert("exceed limit");
}
return false;
});
Here you can see demo: http://jsfiddle.net/yqMGG/91/
You need to compare the numeric values, not the string values. The output of the
.val()function is a DOMString value according to DOM Level 2 which says:so your (5 > 100) test is really
"5" > "100"which is true since strings are compared lexicographically.The solution is to change
to
The
+prefix operator coerces its argument to a number.