Click to See Complete Forum and Search --> : Comparing numeric form values


linnium
05-15-2003, 12:33 PM
I'm using a form validator to make sure the user is not choosing a minimum number that is greater than the maximum number in their search criteria. Here is a sample of the code:

<head>
<script>

function chkSubmit()
{
if (document.formname.minprice.value > document.formname.maxprice.value)
{
alert('The minimum price cannot be greater than the maximum price.');
return false;
}
else
{
return true;
}
}

</script>
</head>
<body>
<form name="formname" action="action.url" method="post" onSubmit="return chkSubmit(this)">

This seemed to work fine, but during testing, when I chose 850,000 as the min and 1,000,000 as the max, submitting triggered the alert and a return of false. I think I understand why this is happening, but I don't know how to do a correct comparison without doing a length count and then adding zero's to the front of the shortes value. Please help.

Thank you.

Jona
05-15-2003, 12:37 PM
You'd have to use if(Number(document.formname.minvalue.value) && Number(document.formname.maxvalue.value)){ if(documen.formname.minvalue > document.formname.maxvalue){ alert("Too much."); return false;}
return true;
}

Unless you want to do this:

if(document.formname.minvalue.value > 1000000 || document.formname.maxvalue.value < 850000){ return false;}

linnium
05-15-2003, 12:44 PM
Ah yes. That did the trick. Thank you very much for your speedy response. Have a good day.

Jona
05-15-2003, 12:45 PM
:confused: You're... Um.... Welcome... :rolleyes: