0

I have this html form that has two inputs. I have onsubmit="formValidate()" which should run that function when the user hits submit. I the function is checking if the two inputs have stuff in them or if they're empty. If they're empty it'll change

to say something like "Not complete" if the inputs are empty or "Complete" if they're filled out. I can't get this to work.

    function formValidate(){
        var form = document.forms["Form"]
          ["name"].value;
            if (name == "", email == "") {
                output = "Not Complete";
            }
            else{
                output = "Complete";
            }
            document.getElementById("test").innerHTML = output;
      }
            <form name="Form" onsubmit="formValidate()">
                <label>Name:</label>
                <input type="text" id="name" name="name"><br/>
                <label>Email:</label>
                <input type="text" id="email" name="email"><br/>
                <button type="submit">Submit</button>
                <p id="test"></p>
            </form>

3 Answers 3

2

There are multiple small problems:

  1. You're not storing values from input fields into variables to check them
  2. validation function must return value true/false to indicate whether form submission should continue
  3. you need to use || instead of , in comparison

here is fixed example:

    function formValidate(){
        var form = document.forms["Form"]
        var name = form.elements["name"].value;
        var email = form.elements["email"].value;
        var result = false;
            if (name == "" || email == "") {
                output = "Not Complete";
            }
            else{
                output = "Complete";
                result = true;
            }
            document.getElementById("test").innerHTML = output;
            return result;
      }
            <form name="Form" onsubmit="return formValidate();">
                <label>Name:</label>
                <input type="text" id="name" name="name"><br/>
                <label>Email:</label>
                <input type="text" id="email" name="email"><br/>
                <input type="submit" value="Submit"/>
                <p id="test"></p>
            </form>

Sign up to request clarification or add additional context in comments.

Comments

0

Use return false; and add return in onsubmit="return formValidate()"

function formValidate(){
        var form = document.forms["Form"]

            if (form.elements["name"].value  == '' || form.elements["email"].value  == '') 
            {
                output = "Not Complete";
                document.getElementById("test").innerHTML = output;
                return false;
            }
            else{
                output = "Complete";
                return true;
            }


      }

Updated JSFiddle

Comments

0

There was a couple problems one is if (name == "", email == "") the or operator is || not ,

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

and you never got the email value you just got the name value.

const test = document.getElementById("test");

function formValidate(event) {
  // stops reload on submit
  event.preventDefault();

  setTestText(isFormInvalid() ? "Not Complete" : "Complete");
}

function getNameAndEmail() {
  const form = document.forms["Form"];
  return {name: form.name.value, email: form.email.value};
}

function setTestText(text) {
  test.innerHTML = text;
}

function isFormInvalid() {
  const {name, email} = getNameAndEmail();
  return (name == "" || email == "");
}
<form name="Form" onsubmit="formValidate(event)">
  <label>Name:</label>
  <input type="text" id="name" name="name"><br/>
  <label>Email:</label>
  <input type="text" id="email" name="email"><br/>
  <button type="submit">Submit</button>
  <p id="test"></p>
</form>

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.