Reputation: 37494
this is not working and i cant really see anything wrong with it, looks straightforward enough:
$("#formLogin").submit(function(){
var username = $("#username").val();
var password = $("#password").val();
if (username == ' ' || password == ' '){
return false;
}
else return true;
});
EDIT:
Its still submitting the form even if the fields are empty
Upvotes: 1
Views: 55989
Reputation: 341
In the html add this
<input type="submit" value="SomeButton" onclick="return submitClicked();" />
in the js do this
function submitClicked() {
if ($("#passwordInput").val() == '')
{
alert('missing password field');
return false;
}
Upvotes: 0
Reputation: 1631
Instead of doing this, why not simply use the jQuery Validation Plugin - http://docs.jquery.com/Plugins/validation
VALIDATE FORMS LIKE YOU'VE NEVER BEEN VALIDATING BEFORE! (According to the site)
Upvotes: 0
Reputation: 103145
Are you trying to detect blank usernames and passwords?
$("#formLogin").submit(function(){
var username = $("#username").val();
var password = $("#password").val();
if (jquery.trim(username) == '' || jquery.trim(password) == ''){
return false;
}
else return true;
});
jquery.trim() removes leading and trailing spaces, so just in case the user types a number of space characters it will be detected.
Upvotes: 11