Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Stopping submit if form invalid

    I wrote a form and a JavaScript to valid the form. I cannot figure out however how to stop the form from submitting if the form is invalid.

    [CODE]
    function validateForm()
    {

    if(""==document.test.custName.value)
    {
    alert("Please enter your name.");
    return false;
    }

    if(""==document.test.email.value)
    {
    alert("Please enter your email address.");
    return false;
    }

    if(""==document.test.custComment.value)
    {
    alert("Please enter your comment.");
    return false;
    }

    return true;
    }
    [CODE]

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Try putting the if's the other way around.

    Code:
    if(document.test.custComment.value === "")
    EXAMPLE:

    Code:
    function validateForm() {
      var e=false,w=[];
      if(document.test.custName.value === '') {
        w[0] = "Please enter your name.";
        e = true;
      }
      if(document.test.email.value === '') {
        w[1] = "Please enter your email address.";
        e = true;
      }
      if(document.test.custComment.value === '') {
        w[2] = "Please enter your comment.";
        e = true;
      }
      if(e) {
        alert(w.join("\n"));
        return false;
      }
    }
    EDIT:
    Are you using the function in a button onclick action or the form onsubmit action?
    Last edited by DJCMBear; 12-24-2010 at 02:19 AM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Arrow

    Quote Originally Posted by Iceman Luch View Post
    I wrote a form and a JavaScript to valid the form. I cannot figure out however how to stop the form from submitting if the form is invalid.

    Code:
    function validateForm() {
      if(""==document.test.custName.value) {
        alert("Please enter your name.");
        return false;
      }
      if(""==document.test.email.value) {
        alert("Please enter your email address.");
        return false;
      }
      if(""==document.test.custComment.value) {
        alert("Please enter your comment.");
        return false;
      }
      return true;
    }
    If using <form> tag:
    Code:
    <form name="my_form" action='' onsubmit="return validateForm">


  •  

    Posting Permissions

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