Like Blender says, for the majority of browser, it is a default behaviour, for an input in the form, to submit the form, when you press enter key (it is like clicking on an input with a submit type).
Your fist code seems a little bit useless because it is preventing the default behaviour (submitting the form), but finally submit the form.
The timeout solution is not bad but too complex in my point of view for a such problem.
Your page is a login page, that meens that you want to allow login try when login and password are filled. Moreover, you don't want to allow multiple submit of the same page in a short delay.
You could write a piece of code like:
// Listening on submit event of the form
$("#FORM_ID").submit(function(submitEvent) {
// Check if the login and password are not empty
if (($.trim($("#LOGIN_INPUT_ID").val())).length == 0 ||
($.trim($("#PASSWORD_INPUT_ID").val())).length == 0)
{
// login or password is empty -> cancel the submit
submitEvent.preventDefault();
// TODO : warn user with a message
// like "Please, fill login and password first !"
return false;
}
// Avoid multiple submit at the same time
// (for stupid guy clicking 10 times in 1 s)
if ($(this).hasData('alreadySubmitted'))
{
// The form is currently already submit -> cancel the submit
submitEvent.preventDefault();
return false;
}
// Set the form "submitted"
$(this).data('alreadySubmitted', true);
// Let the submit do his job...
return true;
});