For a form validation I use a very simple comparism. If y >= x
then it returns false
.
I have solved my problem by using parseInt()
, but I don't understand why I need this function for only ONE value. Eat code:
function validateForm() {
var x = parseInt(document.forms["einstform"]["zeit1"].value);
var y = parseInt(document.forms["einstform"]["zeit2"].value);
if (y >= x) {
alert("Nope." + " " + x + " " + y);
return false;
}
}
And this is my form for that:
<form name="einstform" action="a file" method="GET" onSubmit="return validateForm();">
<select name="zeit1">
<option value="30">1 Month</option>
<option value="60" selected>2 Months</option>
<option value="90">3 Months</option>
</select>
<select name="zeit2">
<option value="0">Never</option>
<option value="7">1 Week</option>
<option value="14" selected>2 Weeks</option>
<option value="21">3 Weeks</option>
<option value="30">4 Weeks</option>
<option value="42">6 Weeks</option>
<option value="60">8 Weeks</option>
<option value="84">12 Weeks</option>
</select>
I guess this is simple to understand. It just compares selected values and cancels the form submit when y >= x
. And it works pretty well, yes. But if I select in the second select "1 Week" (value = 7), the script always tells me 7 >= 30.
Sure, it may get a string value for that 7
, for that I have to use parseInt()
(and with that it works).
But why does the int comparism of all other values work perfectly, except for that 7
?
Is this just random to tell me "always use parseInt()
" or is there more to understand why a 7
refuses to be an int value?