PDA

View Full Version : Javascript problem


XtrmelyCanadian
04-25-2003, 01:32 AM
I am doing some javascript validation for a project and i get an error when trying submit the right values, i get the debug window popping up. I am checking to see if an entered field is a valid price, it does catch the error if i try to submit a invalid price, but when its a valid price a debug window pops up. ANY SUGGESTIONS as to what it could be.

<script language="javascript">
<!--

function Validate(TheForm)
{

if (TheForm.Price.value == "")
{
alert("Please enter a price");
document.TheForm.Price.focus();
return false
}
if (TheForm.YearCur.value == "")
{
alert("Please enter a year");
document.TheForm.YearCur.focus();
return false
}

var Year=document.TheForm.YearCur;
var Price=document.TheForm.Price;
if (isInteger(Year.value)==false)
{
alert("Please enter a valid year eg. 1981");
Year.value="";
Year.focus();
return false;
}
if (isIntegerPrice(Price.value)==false)
{
alert("Please enter a valid year eg. 1981");
Price.value="";
Price.focus();
return false;
}
}
function isIntegerPrice(p)
{
var a;
for (a = 0; a < p.length; a++)
{
var e = p.charAt(i);
if (((e < "0") || (e > "9") || (!e.equals(".")))
{
return false;
}
}
return true;
}

function isInteger(s)
{
var i;
for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (((c < "0") || (c > "9")))
{
return false;
}
}
return true;
}

//-->
</script>

My button calls the validate function, im also checking for a year as well. I bolded the text of the line that seems to be giving me the error.

beetle
04-25-2003, 01:35 AM
Well....function isIntegerPrice(p)
{
var a;
for (a = 0; a < p.length; a++)
{
var e = p.charAt(i);
if (((e < "0") || (e > "9") || (!e.equals(".")))
{
return false;
}
}
return true;
}That's probably because you never define i. I think you wanna use a there instead. :thumbsup:

XtrmelyCanadian
04-25-2003, 01:43 AM
Well that was stupid, i was changing around the variable names just to see if that worked:o . It still didnt seem to fix the problem though it gives me a "Object doesnt support this property or method" error. Any suggestions ????

beetle
04-25-2003, 01:49 AM
well, let's change how you test for an integer, shall we?

There are several ways - here's two

function isIntegerPrice( p )
{
return ( p == parseInt( p ) );
}

function isIntegerPrice( p )
{
return ( /^\d+$/.test( p ) );
}

XtrmelyCanadian
04-25-2003, 01:51 AM
The only problem is, there could be a "." in there as well

beetle
04-25-2003, 02:02 AM
Then why would you call the function isIntegerPrice() ???

Well, if it's a price, let's assume that a decimal at the start is okay, but not at the end. A decimal at the end shoul be followed by at least one digit, no more than two.

return ( /^\d+(\.\d\d?)?$|^\.\d\d?$/.test( p ) );