Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

javascript and error checkin

Options
  • 30-07-2007 4:36pm
    #1
    Registered Users Posts: 36


    Hi just wondering if anyone can spot what im doin wrong here, im tryin to get an alert message if the text field is left empty,
    thanks


    <%@LANGUAGE=&quot;VBSCRIPT"%>
    <script language="javascript" type="text/javascript" >
    function test(objName)
    {
    var checkStr = objName;
    if( checkStr.value.length > "" )
    {
    alertsay = "Please enter only these values"
    alert(alertsay);
    }
    }
    </script>

    <html>
    <body>

    <form action="store.asp" method="post" name="testForm" onSubmit="return test(this)">
    <input name="first" type="text" value="name">
    <input name="submit" type="button" value="sub">
    </form>

    hello <% response.Write(first) %>

    </body>
    </html>


Comments

  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    or89 wrote:
    Hi just wondering if anyone can spot what im doin wrong here, im tryin to get an alert message if the text field is left empty,
    thanks


    <%@LANGUAGE=&quot;VBSCRIPT"%>
    <script language="javascript" type="text/javascript" >
    function test(objName)
    {
    var checkStr = objName;
    if( checkStr.value.length > "" )
    {
    alertsay = "Please enter only these values"
    alert(alertsay);
    }
    }
    </script>

    <html>
    <body>

    <form action="store.asp" method="post" name="testForm" onSubmit="return test(this)">
    <input name="first" type="text" value="name">
    <input name="submit" type="button" value="sub">
    </form>

    hello <% response.Write(first) %>

    </body>
    </html>

    Replace:
    checkStr.value.length > ""
    
    With:
    checkStr.value.length > 0
    

    And replace:
    onSubmit="return test(this)"
    
    With:
    onSubmit="test(this);"
    

    BTW where are you getting the value of the variable 'first' (as in response.write(first))? You never set it anwhere, in fact you have no code to grab the values being sent from the form at all (I'm assuming the code you have posted is the source code of store.asp otherwise apologies for the question)

    -RD


  • Registered Users Posts: 36 or89


    thanks for the help, i took what u said on board, and ya this is the store.asp sorry for not makin it clear, it still is not displaying the error message when the text field is submited empty,

    <%@LANGUAGE=&quot;VBSCRIPT"%>
    <script language="javascript" type="text/javascript" >
    function test(objName)
    {
    var checkStr = objName;
    if( checkStr.value.length > 0 )
    {
    alertsay = "Please enter values greater than zero"
    alert(alertsay);
    }
    }
    </script>

    <html>
    <body>
    <form action="store.asp" method="post" onSubmit="test(this);">
    Your name: <input type="text" name="fname" size="20" />
    <input type="submit" value="Submit" />
    </form>
    <%
    dim fname
    fname=Request.Form("fname")
    If fname<>"" Then
    Response.Write("Hello " & fname & "!<br />")
    Response.Write("How are you today?")
    End If
    %>
    </body>
    </html>


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    "this" is a reserved word, and in the example above will refer to the form, not the field.

    Either replace the parameter for the function call with "fname" including the quotes (recommended in this case) or use the "this" as the base node to find the child input nodes (overkill for this example but would be useful if there were multiple fields within the form)

    One other change required:

    var checkStr = objName;

    should become

    var checkStr = document.getElementById(objName);

    And one final thing - to be cross-browser compatible, you'll need to have an ID as well as a name:

    <input type="text" name="fname" id="fname" size="20" />


  • Registered Users Posts: 36 or89


    :confused: i tried this:

    <%@LANGUAGE=&quot;VBSCRIPT"%>
    <script language="javascript" type="text/javascript" >
    function test(objName)
    {
    var checkStr = document.getElementById(objName);
    if( checkStr.value.length > 0 )
    {
    alertsay = "Please enter values greater than zero"
    alert(alertsay);
    }
    }
    </script>

    <html>
    <body>
    <form action="store3.asp" method="post" onSubmit="test(fname);">
    Your name: <input type="text" name="fname" id="fname" size="20" />
    <input type="submit" value="Submit" />
    </form>
    <%
    dim fname
    fname=Request.Form("fname")
    If fname<>"" Then
    Response.Write("Hello " & fname & "!<br />")
    Response.Write("How are you today?")
    End If
    %>
    </body>
    </html>


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    <script language="javascript" type="text/javascript" >
    function test()
    {
    var checkStr = document.getElementById("fname");
    if( checkStr.value.length > 0 )
    {
    alertsay = "Please enter values greater than zero"
    alert(alertsay);
    }
    }
    

    Replace:
    onsubmit="test(this);"
    With:
    onsubmit="test();"

    Should work now.

    -RD

    BTW onsubmit="test(fname);" is not the same as onsubmit="test('fname');". Be careful when you are passing variables. You still do not have any code retrieving or using the posted form data. You are also referencing a variable that is not set. This code could just as easily be standard HTML for the work it does. What is it you are trying to do? Maybe I can give you a few pointers. Are you just starting to learn ASP?


  • Advertisement
  • Registered Users Posts: 36 or89


    ya just startin to learn it,
    i have a form in a while loop which works well and it interacts with the database grand, but i want to check for errors. So basically i want an alert message if the user submits a empty field.


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    or89 wrote:
    ya just startin to learn it,
    i have a form in a while loop which works well and it interacts with the database grand, but i want to check for errors. So basically i want an alert message if the user submits a empty field.

    I'm baffled by the idea of a form and a while loop. What are you trying to get the page to do?

    -RD


  • Moderators, Science, Health & Environment Moderators Posts: 8,959 Mod ✭✭✭✭mewso


    Aren't we checking for an empty textbox?

    if( checkStr.value.length > 0 )

    should be

    if( checkStr.value.length == 0 )

    I'll skip the my usual rant about doing script in external files, using an onload event to ensure your code is initialised properly and so on.
    I will mention that your original code using "return" was the right way to do this. Basically if the length of the text in the textbox is 0 then you return false, otherwise true. If you don't do this your code runs but the form is submitted whether you want it to be or not.


  • Moderators, Science, Health & Environment Moderators Posts: 8,959 Mod ✭✭✭✭mewso


    Heres an example of doing this kind of thing unobtrusively.

    HTML file:
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head><title>
        Untitled Page
    </title>
        <script type="text/javascript" src="formvalidation.js"></script>
    </head>
    <body>
        <form name="form1" method="post" action="valform.aspx" id="form1">
        <div>
            <input id="first" type="text" value="name" class="required:Please Enter Your Name" />
            <input id="subForm" type="submit" value="Submit" class="submitform" />
        </div>
        </form>
    </body>
    </html>
    

    and the external script file:
    AddLoadEvent(InitFormValidation);
    
    function AddLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                oldonload();
                func();
            }
        }
    }
    
    function InitFormValidation() {
        var inputs = document.getElementsByTagName("input");
        for (i=0;i<inputs.length;i++)    {
            if (inputs[i].className.indexOf("submitform")>-1)  {
                inputs[i].onclick = validateForm;
            }
        }
    }
    
    function validateForm() {
        var inputs = document.getElementsByTagName("input");
        for (i=0;i<inputs.length;i++)    {
            if (inputs[i].className.indexOf("required")>-1)  {
                if (inputs[i].value.length==0)  {
                    alert(inputs[i].className.replace("required:", ""));
                    inputs[i].focus();
                    return false;
                }
            }
        }
    }
    

    The more elements you have in your form the handier this way gets. You could also add different validations. This example just checks for a required field.


Advertisement