Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

javascript problem

  • 07-08-2007 10:47AM
    #1
    Registered Users, Registered Users 2 Posts: 36


    trying to error check, to make sure its only numbers entered where they should be and only letter where there should be letters, nearly there i thing, so if anyone can spot the prob, thanks

    i have all the code here so just copy and paste the 2 files in the same folder and u will c exactly whats wrong,
    thanks again

    <!-- store2.asp -->

    <script language="javascript" type="text/javascript" >
    <!-- Begin
    //Form input validation routine.
    //The function is used to check that user input contains valid information
    //before it is passed to other program logic. The checkNumericForm() function
    //uses the chkNumeric() function to ensure that the each individual input field
    //contains valid information.
    function checkNumericForm(objName,minval, maxval,comma,period,hyphen, text)
    {
    if (chkNumeric(objName.PIN,1000,maxval,comma,period,hyphen, "no") == false)
    {
    objName.PIN.select();
    objName.PIN.focus();
    return false;
    }
    else if (chkNumeric(objName.UserName,minval,maxval,comma,period,hyphen, "yes") == false)
    {
    objName.UserName.select();
    objName.UserName.focus();
    return false;
    }
    else
    {
    return true;
    }
    }

    function chkNumeric(objName,minval,maxval,comma,period,hyphen, text)
    {

    if ( text == "yes" )
    {
    var textCheckOK = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var textCheckStr = objName;
    var textAllValid = true;
    var allText = "";

    for (i = 0; i < textCheckStr.value.length; i++)
    {
    ch = textCheckStr.value.charAt(i);
    for (j = 0; j < textCheckOK.length; j++)
    if (j == textCheckOK.length)
    {
    textAllValid = false;
    break;
    }
    if (ch != ",")
    allText += ch;
    }
    if (!allValid)
    {
    alertsay = "Please enter only these values \""
    alertsay = alertsay + textCheckOK + "\" in the \"" + textCheckStr.name + "\" field."
    alert(alertsay);
    return (false);
    }

    }
    else if ( text == "no" )
    {
    // only allow 0-9 be entered, plus any values passed
    // (can be in any order, and don't have to be comma, period, or hyphen)
    // if all numbers allow commas, periods, hyphens or whatever,
    // just hard code it here and take out the passed parameters.
    // Checks to ensure that only one period character was entered.
    var checkOK = "0123456789" + comma + period + hyphen;
    var checkStr = objName;
    var allValid = true;
    var maxDecPoints = 1;
    var decPoints = 0;
    var allNum = "";

    for (i = 0; i < checkStr.value.length; i++)
    {
    ch = checkStr.value.charAt(i);
    for (j = 0; j < checkOK.length; j++)
    if (ch == checkOK.charAt(j))
    {
    if (ch == ".")
    decPoints++;
    break;
    }
    //Checks to ensure that only one period character was entered.
    if (j == checkOK.length || decPoints > maxDecPoints)
    {
    allValid = false;
    break;
    }
    if (ch != ",")
    allNum += ch;
    }
    if (!allValid)
    {
    alertsay = "Please enter only these values \""
    alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
    alert(alertsay);
    return (false);
    }

    // set the minimum and maximum
    var chkVal = allNum;
    var prsVal = parseInt(allNum);
    if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))
    {
    alertsay = "Please enter a value greater than or "
    alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
    alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
    alert(alertsay);
    return (false);
    }
    }
    }
    // End -->
    </script>

    <html>
    <body>
    <form action="store3.asp" method="post" name="Form1" onSubmit="return checkNumericForm(this,0,9999,' ','.',' ',' ')">
    Your name: <input name="UserName" type="text">
    PIN: <input name="PIN" type="text">
    <input type="submit" value="Submit" />
    </form>

    </body>
    </html>


    <!-- store3.asp -->

    <%
    dim fname
    fname=Request.Form("UserName")
    dim num
    num=Request.Form("PIN")
    If fname<>"" Then
    Response.Write("Hello " & fname & "!<br />")
    Response.Write("How are you today?")
    Response.Write("Your PIN is" & num)
    End If
    %>


Comments

  • Registered Users, Registered Users 2 Posts: 6,651 ✭✭✭daymobrew


    I recommend using regular expressions. To check for numbers your regular expression will probably be /^\d+$/ (string must be 1 or more numbers) and for letters /^[a-zA-Z]+$/ (string must 1 or more word chars).


  • Registered Users, Registered Users 2 Posts: 36 or89


    thanks for your time and help:o


  • Registered Users, Registered Users 2 Posts: 6,651 ✭✭✭daymobrew


    You didn't list the errors you are getting.
    Have you run the code through a debugger? Put a load of alert() messages into the code.


Advertisement