Results 1 to 3 of 3

Thread: Page being submitted after error catch

  1. #1
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Page being submitted after error catch

    I have a form validation script that seems to be working. However, when it does its job and catches an error, my page still gets submitted.

    How to I keep it from being submitted until all fields are correctly entered?

    Here's the event handler: <form id="private" method="post" action="contact.php" class="form" onSubmit="return ValidateData(this)">. Script is below.

    Thx,

    Rick

    <script language="javascript">
    function ValidateData(form)
    {
    if (!form.name.value)
    {
    alert("\nName is a required field.");
    form.name.focus();
    return (false);
    }
    var email = form.email.value;
    if (!email)
    {
    alert("\nEmail Address is a required field.");
    form.email.focus();
    return (false);
    }

    if ((email.indexOf(" ") > -1) || (email.indexOf("@") == - 1) || (email.indexOf(".") == -1))
    {
    alert("Please enter a valid Email Address.");
    form.email.focus();
    return (false);
    }

    if (!form.message.value)
    {
    alert("\nMessage is a required field.");
    form.message.focus();
    return (false);
    }
    return (true);
    }
    </script>

  2. #2
    Join Date
    Feb 2007
    Posts
    293
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You've basically created an if/else statement, but forgot the else. The last line should be
    Code:
    else {return true}
    Otherwise, you've told it to return true even if the other conditions are present.

  3. #3
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    should have caught that one... thanks!

Bookmarks

Posting Permissions

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