Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
151
rated 0 times [  157] [ 6]  / answers: 1 / hits: 41862  / 13 Years ago, tue, january 10, 2012, 12:00:00

I need to submit the content of a form when I press the Enter key, but only if the form has no error message. I built up the following function:



$(targetFormID).submit(function (e) {
var mess = error_m(targetDiv);
if (e.keyCode == 13 && mess.length > 0) {
e.preventDefault();
e.stopPropagation();
}
if (mess.length == 0 && e.keyCode == 13) $(targetFormID).submit();
});


In this function the mess variable is getting the error message returned by function error_m, the rest is simple code condtion but it doesn't work.



Need some help with this!!


More From » jquery

 Answers
5

Submitting the form when the Enter key is pressed is default browser behaviour. Don't mess with it. Just validate the form in the submit event.



$(targetFormID).submit(function (e) {
var mess = error_m(targetDiv);
if (mess.length > 0) {
e.preventDefault();
}
});


One other possible problem: what is targetFormID? If it's actually a string containing an element ID, you'll need



$(# + targetFormID).submit(/* Same function as above */);


If it's a reference to the form element then $(targetFormID) is fine but your variable is misleadingly named.


[#88115] Monday, January 9, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
heidys

Total Points: 665
Total Questions: 102
Total Answers: 97

Location: Belarus
Member since Sat, Jul 18, 2020
4 Years ago
;