Reputation: 2799
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.
Upvotes: 0
Views: 177
Reputation: 26997
When you return false
, you're actually preventing the default behavior (submission in this case).
Upvotes: 0
Reputation: 220
Assuming that your validation code is called on a onSubmit event chain such as in
$('form').onSubmit(function(event) {
// do validation
return false;
}
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 :
$('a').click(function(event) {
event.preventDefault();
return false;
}
Upvotes: 1