0

I'm trying to make a working javascript validator for an HTML form for an assessment, I have gotten the lecturer to help me originally, however they could not solve the issue. The validation works until I add another input to validate then it completely stops working. I am new to javascript, i have never worked with it before.

I have tried rebuiling the validator and order form completely multiple times. I have reviewed the code multiple times to no avail.

function validateForm() {
  var x = document.forms["myForm"]["fname"].value;
  if (x == " ") {
    alert("Name must be filled out");
    return false;
  }
  var y = document.forms["myForm"]["mname"].value;
  if (y == " ") {
    alert("middle name must be filled out");
    return false;
  }
}
<script src="Validator.js"></script>
<form name="myForm" onsubmit="return validateForm()" action="OrderConfirmed.htm" method="post">
  Name: <input type="text" name="fname" value=" "> Middle Name: <input type="text" name="mname" value=" ">
  <input type="submit" value="Submit">
</form>

The expected result is that when the form is submitted but a field is empty a message (dialog box) should be displayed telling the user which input is not filled in.

9
  • The code you've posted works fine (though you should compare to an empty string "") - can you please post the code that fails? Commented Jun 15, 2019 at 14:42
  • That's odd because whenever i attempt to submit the form it fails to validate and lets through empty results Commented Jun 15, 2019 at 14:45
  • Do you get any error in the console? Commented Jun 15, 2019 at 14:45
  • Using the chrome console i don't get any errors at all Commented Jun 15, 2019 at 14:50
  • This is likely something to do with posting to action="OrderConfirmed.htm" but we really cannot clearly tell here. I deleted my answer as it really did not address that. Commented Jun 15, 2019 at 14:51

1 Answer 1

1

You could solve it by using 'event.preventDefault'. (But your code should work too...) Here is example code:

<html>
<head>
    </head>
    <body>
	<form name="myForm" onsubmit="validateForm(event)">
	    Name: <input type="text" name="fname" value=" ">
	    Middle Name: <input type="text" name="mname" value=" ">
	    <input type="submit" value="Submit">
	</form>
	<script type="text/javascript">
	    function validateForm(e) {
                var x = document.forms["myForm"]["fname"].value;
                if (x == " ") {
                    e.preventDefault();
                    alert("Name must be filled out");
                    return false;
                }
                var y = document.forms["myForm"]["mname"].value;
                if (y == " ") {
                    e.preventDefault();
                    alert("middle name must be filled out");
                    return false;
                }
            }
        </script>
     </body>
</html>

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

1 Comment

No worries, though your code should work. Maybe there is something with including your script.

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.