SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why does this JS form validation script.....

    ....send the data anyways, after errors have been reported via alert?

    I have written a few PHP form validator scripts, but I need to have this one work fully in JS. The only problem with the below code is, is that when the errors are reported via alert, an alert comes up after that saying if I want to send the email.

    Here is the code:
    Code:
    <Script language="JavaScript">
    
    function checkForm() {
    
    var invaliddata = "";
    
    if (document.form1.name.value == "") {
    invaliddata += "\n     -Name-";
    }
    if ((document.form1.email.value == "") || 
    (document.form1.email.value.indexOf('@') == -1) || 
    (document.form1.email.value.indexOf('.') == -1))
    {
    invaliddata += "\n     -Email address-";
    }
    
    if(document.form1.message.value == "")
    {
    invaliddata += "\n     -Message-";
    }
    
    if (invaliddata != "")
    {
    invaliddata ="Errors have been detected in your submission:\n" + invaliddata;
    alert(invaliddata);
    
    }
    }
    
    </script>
    </HEAD>
    <BODY>
    <form name="form1" action="mailto:host@html4me.com?subject=Form from my website" method="POST" onSubmit="checkForm()">
      <br>
        Name: 
        <input type="text" name="name">
        <br>
        Email: 
        <input type="text" name="email">
        <br>
        <br>
        Message: <br>
        <textarea rows="4" cols="35" name="message"></textarea>
        <br>
        <input type="submit" name="submit" value="Submit form">
    
      </form>
    There are other scripts like this (JS form validators) which are much more complicated, but they work-- and do not opt to send the email if there is an error in the form.

    So how do I make it so the script doesn't opt to send the form data if an error has been found?

    For example, this form validation script doesn't have the same problem as I:

    http://javascript.about.com/library/...rmvalidate.htm

    Well, thanks in advance--

    Gregc

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your form you need to change
    onSubmit="checkForm()"
    to
    onSubmit="return checkForm()">

    Then in the script...

    if (invaliddata != "")
    {
    invaliddata ="Errors have been detected in your submission:\n" + invaliddata;
    alert(invaliddata);
    return false; // this is the new line you need to add
    }

  3. #3
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!!

    It works perfectly now.

    Gregc


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
  •