...

View Full Version : Checking for Correct Number



redraider
01-17-2005, 03:33 AM
Hello, I am trying to use javascript to check a number to make sure it is valid or change it if it is not (meaning it has letters or other characters). I am getting stuck with the check. The number needs to be only 5 digits. Any help is appreciated!

Thanks!


<script language="JavaScript">
<!--
var numbercookie = getCookie("number");
var numbergood = "false";
if (!numbercookie) {
numbercookie = prompt("Please enter your number:", "");
while (numbergood == "false") {
if (numbercookie.length < 5)
{
numbercookie = prompt("Please enter a number using a valid format:", "");
}
if (numbercookie.length = 5)
{
for (var i=0; i < numbercookie.length; i++)
{
if (invalid.indexOf(numbercookie.charAt(i)) >= 0)
{
numbergood = "true";
}
}
numbercookie = prompt("Please enter a number using a valid format:", "");
}
}
}
}
setCookie("number", numbercookie,0);

}
// -->
</script>

Thanks!

Brandoe85
01-17-2005, 03:37 AM
You probably want to look into isNaN() (http://www.devguru.com/Technologies/ecmascript/quickref/isnan.html)

redraider
01-17-2005, 04:45 AM
Thanks for the advice. Not sure what I'm doing wrong. Doesn't work correctly. Specifically the "while" loop. I am able to enter any number regardless of whether it is 5 digits or characters. Could someone please check me.

Thanks!

<script language="JavaScript">
<!--
var numbercookie = getCookie("number");
var numbergood = "false";
if (!numbercookie) {
numbercookie = prompt("Please enter your number:", "");
}
while (numbergood == "false") {
if (numbercookie.length = 5)
{
if (isNaN(numbercookie))
{
numbercookie = prompt("Please enter a number using a valid format:", "");
} else numbergood = "true";
} else numbercookie = prompt("Please enter a number using a valid format:", "");
}
// -->
</script>

redraider
01-17-2005, 05:11 AM
I checked my syntax and found an error. It works great now.

Thanks for the help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum