Results 1 to 2 of 2

Thread: JavaScript: Form Validation

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    JavaScript: Form Validation

    What's wrong with this code?:

    Code:
    <script>
        function check(f) {
            if (f.title.value == "") {
                alert("You must have a subject.");
            } elseif (f.body.value == "") {
                alert("You must have a body in your post.");
            } elseif (f.body.value.length > 10000) {
                alert("Your message is too long. The max is 10000 characters.");
            } else {
                 document.f.submit();
            }
        }
    </script>
    <form action="post.php" method="post">
    ...
    <input type="button" value=" Submit " onClick="check(forms(0))">
    It's just supposed to validate the form and make sure all the fields are filled.
    Last edited by The Hobo; Aug 11th, 2002 at 12:36 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I got this working:

    Code:
    <script language="JavaScript" type="text/javascript">
        function check() {
            f = document.postform;
            if (f.title.value == "") {
                alert("You must have a subject.");
                event.returnValue = false;
            } else if (f.body.value == "") {
                alert("You must have a body in your post.");
                event.returnValue = false;
            } else if (f.body.value.length > 10000) {
                alert("Your message is too long. The max is 10000 characters.");
                event.returnValue = false;
            } else {
                event.returnValue = true;
            }
        }
    </script>
    Can anyone tell me if it's cross browser compliant?
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width