0

I'm having to dip my toe back in the EE and javascript world after many years. What am I doing wrong here... a simple form being submitted, with a JS block to validate during the onsubmit event of the submit button. The alert("Here") message box does not appear when I submit the form, and the form gets submitted successfully, i.e. HelloForm gets displayed, with blank values, so I'm led to believe that validate() is not being called. I've tried this in both the Eclipse internal web browser as well as Chrome and see the same thing both times. I'm assuming that I don't need to explicitly activate javascript in either browser, as it would be on by default.

<html>
<head>
<script>
function validate()
{
    alert("Here");
    var x = document.forms["inputForm"].["first_name"].value;
    if (x == null || x == "") {
        alert("First name must be filled out");
        return false;
    }
    return true;

}
</script>
</head>
<body>

<form name="inputForm" action="HelloForm" method="GET" onsubmit="return validate()">
First Name: <input type="text" name="first_name">
<br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit The Form"  />
</form>
</body>

</html>
1
  • try to call this validate(); function on Button Click Commented Jun 26, 2015 at 19:08

2 Answers 2

1

In this line will throw a syntax error:

document.forms["inputForm"].["first_name"].value
                          ^^^

The statement is wrong, should be without dot between braces:

document.forms["inputForm"]["first_name"].value
//or
document.forms.inputForm.first_name.value

Working with Objects

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

Comments

0

You can use Html5 Validations.

<form name="inputForm" action="HelloForm" method="GET" >
    First Name: <input type="text" name="first_name" required />
<br />
Last Name: <input type="text" name="last_name" required />
<input type="submit" value="Submit The Form"  />
</form>

Working Demo

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.