www.webdeveloper.com
Results 1 to 4 of 4

Thread: XHTML Javascript validation problem

  1. #1
    Join Date
    Jul 2010
    Posts
    3

    XHTML Javascript validation problem

    I am attempting to convert a HTML form to XHTML. I am having problems with the validation script, which worked in the old form but not in the new one. The script makes sure that all the boxes are filled out before submitting.
    I have both <input> and <textarea> elements. The script recognizes a blank <input> but not the blank <textarea>.
    What's wrong?

    Javascript snippets:

    <script type="text/javascript">

    function verify()
    {
    if(document.forms['formname'].aname1.value=="")
    {
    alert("Please enter your name");
    document.forms['formname'].aname1.focus();
    return false;
    }
    [snip]
    if(document.forms['formname'].gaddress.value=="")
    {
    alert("Please enter your mailing address");
    document.forms['formname'].gaddress.focus();
    return false;
    }
    [snip]
    else
    return true;
    }

    </script>

    XHTML snippets:

    <form action="/formmail.cgi" id="formname" method="post" onsubmit="return verify()">
    [snip]
    <table summary="" style="width:100%; cellpadding:0; cellspacing:0; border:0">
    <tr>
    <td class="td10">
    Name
    </td>
    <td class="td90">
    <input name="aname1" type="text" size="100%" />
    </td>
    </tr>
    <tr>
    [snip]
    <tr>
    <td class="td10">
    Mailing<br />
    Address
    </td>
    <td class="td90">
    <textarea name="gaddress" rows="3" cols="87">
    </textarea>
    </td>
    </tr>
    </table>
    [snip]
    </form>

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    I'm not sure what the problem is

    The code below is your code with the [snip]s removed and your validation script prompts for both the name and email address if they are missing - but with your current code logic, only 1 at a time if both are missing.

    The only thing I would normally do, but haven't done in your case, is put the opening and closing textarea tags on the one line.

    Your code works fine in my IE8 and FF3.6

    Maybe there is something else going on in your code that you haven't posted that is causing your problem.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    >
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    title></title>
    <
    script type="text/javascript">
    function 
    verify()
    {
    if(
    document.forms['formname'].aname1.value=="")

    alert("Please enter your name");
    document.forms['formname'].aname1.focus();
    return 
    false;
    }
    if(
    document.forms['formname'].gaddress.value=="")

    alert("Please enter your mailing address");
    document.forms['formname'].gaddress.focus();
    return 
    false;
    }
    else
    return 
    true;

    </script>
    </head>
    <body>
    <form action="#" id="formname" method="post" onsubmit="return verify()">
    <table summary="" style="width:100%; cellpadding:0; cellspacing:0; border:0">
    <tr>
    <td class="td10">
    Name
    </td>
    <td class="td90">
    <input name="aname1" type="text" size="100%" /> 
    </td> 
    </tr>
    <tr>
    <td class="td10">
    Mailing<br />
    Address
    </td>
    <td class="td90">
    <textarea name="gaddress" rows="3" cols="87">
    </textarea> 
    </td>
    </tr>
    </table>
    <input type="submit" value="submit" />
    </form>
    </body>
    </html> 

  3. #3
    Join Date
    Jul 2010
    Posts
    3
    Now I am really puzzled. As you entered the code it does work. But in my original the same code skips the textarea. I'll dig deeper to see if I can figure out where the error is.
    Thanks.

  4. #4
    Join Date
    Jul 2010
    Posts
    3
    I recreated a new page from scratch. Now it works. The code appears to be identical on both pages, One works, and one doesn't. Go figure.
    Thanks for your help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles