1

I have nearly 5 forms inside my webpage whose elements are validated using jquery.validate.js. And I have 'save' button(not submit) at the end which on clicking saves all detail. I need to validate all the forms when I click on 'save' and if any validation fails I need to stop that click event.Is there a way to do it?

for example :

<html>
<script type='text/javascript'>
    $(document).ready(function () {
        $('#form1').validate();
        $('#form2').validate();
        $('#form3').validate();
    });
</script>
<form id='form1'>..elements to be validated..</form>
<form id='form2'>..elements to be validated..</form>
<form id='form3'>..elements to be validated..</form>
<form id='form4'>..elements to be validated..</form>...
<input type='button' name='save_btn' id='save_btn' value='save'>
</html>

Also I tried

$("#save_btn").click(function(){
    $("form1").validate();
});

It displays error message but still click function is carried out.If anybody can suggest me some solution it will be more helpful form me.

Thank you.

1 Answer 1

5

you could use the function valid() that is available to check whether a form or a particular element is valid

so you could do something like this for your button click event

$("#save_btn").click(function(){
  if ($("form1").valid()
        && $("form2").valid()
        && $("form3").valid()
        && $("form4").valid())
  {
     // do what you want here
  } else {
     return false; // to suppress the click event
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Lawrence Leung ,it helped me and it is working fine now..I was not aware of valid() function earlier..
@Santhiya you are welcome. Keep this handy, it has all the functions available to you.

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.