Search code examples
jqueryvalidationsubmithref

A href executed if the validation successful - JQuery


I am trying to let a href work if the fields are validated, so I set an a link not populated such as:

     <input id="email" placeholder="Email" type="text">
     <a type="submit" href="{% register %}" class="btn submit">
     </a>

In Jquery

$('.submit').click(function(e) {
   e.preventDefault();

   if (validate()) {
     <!-- not sure how to submit the href here -->
   }

});

Btw, href is part of Django.


Solution

  • Don't target the button, target the form and prevent the default action inside the if:

    $('#your-form-id').submit(function(e) {    
       if (!validate()) {
         e.preventDefault();
         return;
       }
    });
    

    Here's an example of form validation