backt next

Working with Forms


In the following form if you leave the required sections blank, as you click submit, you will be forced to come back and fill this box in.

Name: (* required)
Comment: (*required)
Email:


The above form was created using the following code

<form name="example" onSubmit="return validate()">
Name: <input type="text" size=20 name="name"> (* required)
Comment: <textarea name="comment" rows=3 cols=30> (*required)
Email: <input type="text" name="email" size=20>
<input type="submit" name="submit" value="Submit">
</form>

<script language=JavaScript>
<!--
function validate()
{
if((document.example.name.value=="") || (document.example.comment.value==""))
{
alert("You must fill in all of the required fields!")
return false
}
}
//-->
</script>


What's 'return true', 'return false' ? This is what's used to actually allow, or stop the form from submitting, respectively. This is how JavaScript controls the submitting of a form. By default, a form will return true (submit the form).

I create function validate() that only returns true/false. You need return true/false to actually manipulate whether a form submits or not.

|| = or

onSubmit is the event that invoke JavaScript upon clicking submit button.



Back to JavaScript homepage | Back | Next | Email me