the html code:
<labelName</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input" />
<label class="error" for="name" id="name_error">This field is required.</label>
the js code:
var name = $("input#name").val();
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}
why use the return false;? could i use exit? thank you.
Assuming that your validation code is called on a onSubmit event chain such as in
return false is used to block the form submission to continue. Without it the form submission will continue and the action page will be loaded.
other then “return false;”, you could also add “event.preventDefault();” for blocking the form submission.
The same method could also be used in other event. For example, if you want to block a click event from a link (which by default would load the page specified on href), you could do :