0

On my MVC project I have a View with 2 forms.

I validate the forms using jquery validation.

How can I validate 2 forms on submit of one of them?

I tried that:

$(function () {
     $("#form1").validate();
     $("#form2").validate();
     $.extend($.validator.messages, { required: "*", email: "Invalid email address." })
});

But only the second form gets validate but the first one don't, any idea on how to solve that?

7
  • @mplungjan How this question is a duplicate of the one you referred to? I'm talking about 2 forms? !! Commented Feb 27, 2017 at 10:09
  • Because: $("#form1").on("submit",function(e) { if ($("#form1").valid() && $("#form2").valid()) { console.log("great job"); else e.preventDefault(); }); Commented Feb 27, 2017 at 10:10
  • This is an answer to my question, not a duplicate of the other question. Commented Feb 27, 2017 at 10:11
  • If you know how to test ONE form is valid, you know how to test TWO forms are valid! Commented Feb 27, 2017 at 10:12
  • 1
    @mplungjan Sure! :) Commented Feb 27, 2017 at 10:25

1 Answer 1

2

Try

$("#form1").on("submit",function(e) { 
  if ($("#form1").valid() && $("#form2").valid()) { 
    console.log("great job"); 
  else e.preventDefault(); 
});

For alternatives

Validating multiple forms on the same page

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.