...

View Full Version : validation is getting confused



npala2001
01-09-2007, 04:41 PM
The validation i am using is getting confused when it gets a number that has more than three decimal points (ex: 1.000)

Below is my code - the form works


<script language="javascript">
function doValidate()
{
var theForm = document.myForm;

if (theForm.Field_1.value < theForm.Field_2.value || theForm.Field_1.value == '' || theForm.Field_2.value == '')
{
alert("You must enter a bigger size");
theForm.Value8.focus();
return (false);
}
if (theForm.Field_2.value < theForm.Field_3.value || theForm.Field_2.value == '' || theForm.Field_3.value == '')
{
alert("You must enter a bigger size");
theForm.Value7.focus();
return (false);
}
return (true);
}
</script>

<form name="myForm" onSubmit="return doValidate(this)">
<table>
<tr>
<th align="right"><span class="style1">Field 1</span></th>
<td><input type="text" name="Field_1" size="30"></td>
</tr>
<tr>
<th align="right"><span class="style1">Field 2</span></th>
<td><input type="text" name="Field_2" size="30">
** </td>
</tr>
<tr>
<th align="right"><span class="style1">Field 3</span></th>
<td><input type="text" name="Field_3" size="30">
** </td>
</tr>
</table>
</form>

david_kw
01-09-2007, 05:08 PM
It seems to work to me. Exactly what inputs are failing for you?

Here is my test code.



<html>
<head>
<script language="javascript">
function doValidate()
{
var theForm = document.myForm;

if (theForm.Field_1.value < theForm.Field_2.value || theForm.Field_1.value == '' || theForm.Field_2.value == '')
{
alert("You must enter a bigger size");
// theForm.Value8.focus();
return (false);
}
if (theForm.Field_2.value < theForm.Field_3.value || theForm.Field_2.value == '' || theForm.Field_3.value == '')
{
alert("You must enter a bigger size");
// theForm.Value7.focus();
return (false);
}
alert("validated");
return (false);
}
window.onload = function () {
document.myForm.Field_1.value = "7.00000";
document.myForm.Field_2.value = "5.00001";
document.myForm.Field_3.value = "5.00000";
}
</script>
</head>

<body>
<form name="myForm" onSubmit="return doValidate(this)">
<table>
<tr>
<th align="right"><span class="style1">Field 1</span></th>
<td><input type="text" name="Field_1" size="30"></td>
</tr>

<tr>
<th align="right"><span class="style1">Field 2</span></th>
<td><input type="text" name="Field_2" size="30">
** </td>
</tr>
<tr>
<th align="right"><span class="style1">Field 3</span></th>
<td><input type="text" name="Field_3" size="30">
** </td>

</tr>
</table>
<input type="submit" />
</form>
</body>
</html>


david_kw

npala2001
01-09-2007, 05:21 PM
Field 1 = 12.25

Field 2 = 9.625

Field 3 = 5.5

I have the input types as text fields so I am thinking that is why its treating them differently. It looks like its getting locked up on Field 1 and 2. Is there a way to get the javascript to treat the field as numeric data

npala2001
01-09-2007, 05:29 PM
Your code is different I have an extra return true statement at the bottom

SpirtOfGrandeur
01-09-2007, 06:02 PM
Yes you are comparing strings. Of course it would do that...

Google Javascript and parseInt

david_kw
01-09-2007, 06:19 PM
Good call on parseFloat



<html>
<head>
<script language="javascript">
function doValidate()
{
var theForm = document.myForm;

if (parseFloat(theForm.Field_1.value) < parseFloat(theForm.Field_2.value) || theForm.Field_1.value == '' || theForm.Field_2.value == '')
{
alert("You must enter a bigger size");
// theForm.Value8.focus();
return (false);
}
if (parseFloat(theForm.Field_2.value) < parseFloat(theForm.Field_3.value) || theForm.Field_2.value == '' || theForm.Field_3.value == '')
{
alert("You must enter a bigger size");
// theForm.Value7.focus();
return (false);
}
alert("validated");
return (false); // changed from true so it won't reset the page for testing
}
window.onload = function () {
document.myForm.Field_1.value = "12.25";
document.myForm.Field_2.value = "9.625";
document.myForm.Field_3.value = "5.5";
}
</script>
</head>

<body>
<form name="myForm" onSubmit="return doValidate(this)">
<table>
<tr>
<th align="right"><span class="style1">Field 1</span></th>
<td><input type="text" name="Field_1" size="30"></td>
</tr>
<tr>
<th align="right"><span class="style1">Field 2</span></th>
<td><input type="text" name="Field_2" size="30">
** </td>
</tr>
<tr>
<th align="right"><span class="style1">Field 3</span></th>
<td><input type="text" name="Field_3" size="30">
** </td>
</tr>
</table>
<input type="submit" />
</form>
</body>
</html>


david_kw

npala2001
01-09-2007, 06:44 PM
MAN I LOVE YOU GUYS!!!! It worked like a champ and I learned something today.

SpirtOfGrandeur
01-09-2007, 08:18 PM
ehehe parseFloat... whoops for some reason I was thinking about Integers... sorry!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum