Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checking for Correct Number

    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!

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You probably want to look into isNaN()

  • #3
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Modified code still not working

    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>

  • #4
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Got it working!

    I checked my syntax and found an error. It works great now.

    Thanks for the help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •