Klapsius
Klapsius

Reputation: 3369

form submit not working correct with jquery

i have ajax validation function and when ajax validation is true then i use:

$("#test_form").submit();

it is work fine but any time i can click Enter and submit a form when form is not validated (skip validation process and pass wrong data). i try put

$("#test_form").submit(function(){event.preventDefault();});

then still can submit with enter when data is wrong or

$("#sample_form").submit(function(){false});

then i cant submit any time.

How to submit only when ajax is true?

Upvotes: 3

Views: 90

Answers (2)

Karl-André Gagnon
Karl-André Gagnon

Reputation: 33880

The first part of your answer is said in the comment section :


Use $("#test_form").submit(function(event){event.preventDefault();}); notice function(event)

– Satpal


Then you'll notice an other problem, your form will never submit after the AJAX call. That is because you use $("#test_form").submit(); to submit, which is the jQuery triggering the event. When jQuery trigger the event, it will always be prevented by the preventDefault.

What you need to do is to use the native JavaScript event :

$("#test_form")[0].submit();

When you are using the native handler, the event you have been added with jQuery will not trigger. It will instead directly send the form.

Upvotes: 1

Amit
Amit

Reputation: 15387

In Submit button, Use onclick with return then On press enter it will check first javascript validation.

Upvotes: 0

Related Questions