Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire jQuery function only if form is valid

I have a jQuery function tied to my submit button like this:

$(function () {     $('#signupform').submit(function () {         alert('test');     }); }); 

However, it fires whether or not the form is valid. My model is decorated with various DataAnnotations and the client-side validation is working well, but I only want that jQuery function to fire if the form has been validated. How do I accomplish that?

EDIT: To clarify, I'm using MVC DataAnnotations + jQuery's unobtrusive javascript to handle the client-side validation. I do not have my own javascript validation routines written. The built in jQuery validation is doing a great job of validating the form, but I just need to know how to get the results of that validation into a boolean value within my own function.

like image 568
Scott Avatar asked Feb 19 '11 17:02

Scott


People also ask

How do you check whether a form is valid or not in jQuery?

$("#form_id"). valid(); Checks whether the selected form is valid or whether all selected elements are valid. validate() needs to be called on the form before checking it using this method.

How do you check if a form is valid?

Using the email type, we can check the validity of the form field with a javascript function called… checkValidity() . This function returns a true|false value. checkValidity() will look at the input type as well as if the required attribute was set and any pattern="" tag .

How do I stop a form from submitting in jQuery?

We use the preventDefault() method with this event to prevent the default action of the form, that is prevent the form from submitting.

Why jQuery validation is not working?

validate is not a function" jQuery error occurs when the jQuery validation plugin is not loaded or the jQuery-related scripts are loaded in an incorrect order. The validation plugin should be loaded after the jQuery library.

How to validate if form is valid using jQuery?

If you are using jquery validate unobtrusive validation you could: $(function () { $('#signupform').submit(function () { if($(this).valid()) { alert('the form is valid'); } }); });

Does $ (function () ) fire if the form has been validated?

$(function () { $('#signupform').submit(function () { alert('test'); }); }); However, it fires whether or not the form is valid. My model is decorated with various DataAnnotations and the client-side validation is working well, but I only want that jQuery function to fire if the form has been validated.

Is jQuery validate a “must-have” plugin?

Let us see how this Plugin easy-fy your form Validation and prove to be a “must-have” Plugin? The jQuery Validate is a feature provided by jQuery that makes it easier for the creator to validate the user or visitor’s input data while keeping your code free of all the mesh of validation rules from javaScript code.

Is there a way to stop form submitting on validation?

Note, the validateForm()function would be the one you use within the browser to test validation. EDIT: This is indeed jQuery's $(this).valid(). NOTE: On clarification of the question, the answer above is one way to stop form submitting on validation, but not the technique that was desired (jQuery valid() validation).


2 Answers

If you are using jquery validate unobtrusive validation you could:

$(function () {     $('#signupform').submit(function () {         if($(this).valid()) {             alert('the form is valid');         }     }); }); 
like image 144
Darin Dimitrov Avatar answered Oct 14 '22 13:10

Darin Dimitrov


$(function () {     $('#signupform').submit(function (e) {         if (validateForm() === true) {             alert('Form is valid.');         }     }); }); 

Note, the validateForm() function would be the one you use within the browser to test validation. EDIT: This is indeed jQuery's $(this).valid().

NOTE: On clarification of the question, the answer above is one way to stop form submitting on validation, but not the technique that was desired (jQuery valid() validation). See Darin Dimitrov's answer for details.

Also, on rereading the original question, the intent was the exact opposite of what I originally described. So I edited to demonstrate a functional response on validation, not how to prevent submitting a form.

like image 43
Jared Farrish Avatar answered Oct 14 '22 14:10

Jared Farrish