skip to Main Content

I use jQuery validator plugin from this link :

https://www.jqueryscript.net/form/instant-input-validation.html#google_vignette

Everything works fine, but when I press submit button, I want get result from

$("#validate").inputValidation();

To make sure everything is correct.
How can this be done?

Thanks ,

2

Answers


  1. Use the function valid() after validating the form with validate.

    Hope this helps!

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.validate.min.js"></script>
    <link rel="stylesheet" href="https://jqueryvalidation.org/files/demo/site-demos.css">
    
    
    <form class="cmxform" id="myForm" method="get" action="">
      <fieldset>
        <legend>Please provide your name, email address (won't be published) and a comment</legend>
        <p>
          <label for="cname">Name (required, at least 2 characters)</label>
          <input id="cname" name="name" minlength="2" type="text" required>
        </p>
        <p>
          <label for="cemail">E-Mail (required)</label>
          <input id="cemail" type="email" name="email" required>
        </p>
        <p>
          <label for="curl">URL (optional)</label>
          <input id="curl" type="url" name="url">
        </p>
        <p>
          <label for="ccomment">Your comment (required)</label>
          <textarea id="ccomment" name="comment" required></textarea>
        </p>
        <p>
          <button type="button">Validate!</button>
        </p>
      </fieldset>
    </form>
    <script>
      jQuery.validator.setDefaults({
        debug: true,
        success: "valid"
      });
      var form = $("#myForm");
      form.validate();
      $("button").click(function() {
        alert("Valid: " + form.valid());
      });
    </script>
    Login or Signup to reply.
  2. Typically, the validation method works something like this:

    $("#submitBtn").click(function() {
      if ($("form#validate").valid()) {
        // Form is valid, perform your desired action
        console.log("Form is valid");
      } else {
        // Form is not valid, handle the validation errors
        console.log("Form is not valid");
      }
    });
    

    You did not provide any details on your code, so the issue might potentially be in you not including the library properly, it’s hard to tell

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search