Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 required and jQuery submit()

Tags:

html

jquery

I'm trying to implement html5 into a form, but I came with a problem when I submit the form through jquery and try to use the html5 "required" attribute.

Here is my form, quite simple:

<form action="index.php" id="loginform">       <input name="username" required placeholder="username" type="text">           <a href="javascript:$('#loginform').submit();">Login</a> </form> 

The problem is that when the form is submited through jquery, It just by-passes the required attribute. I know that required should only work if the form is submitted by the user, not by a script, so when I change the anchor to a submit input it works perfect.

My point is if there is a way to force the jquery .submit() to use html5 required atribute.

Thanks in advance for the answers

like image 599
Dan Stern Avatar asked Jul 08 '12 07:07

Dan Stern


People also ask

How do I force a HTML5 form validation without submission?

on('click', function(event) { var isvalidate = $("#formID")[0]. checkValidity(); if (isvalidate) { event. preventDefault(); // HERE YOU CAN PUT YOUR AJAX CALL } }); }); Code described above will allow You to use basic HTML5 validation (with type and pattern matching) WITHOUT submitting form.

What does jQuery submit do?

jQuery submit() Method The submit event occurs when a form is submitted. This event can only be used on <form> elements. The submit() method triggers the submit event, or attaches a function to run when a submit event occurs.

Can we submit form in jQuery?

Answer: Use the jQuery submit() Method You can use the submit() method to submit an HTML form (i.e. <form> ) using jQuery. The jQuery code in the following example will submit the form on click of the button (i.e. the <button> element) which has the type attribute set to button (i.e. type="button" ).

Does a form require a submit button?

If you don't have any submit button it is acceptable after all it is an element of form tag and if it is not required you may not add it with in form . This will not broke any web standard.


2 Answers

I did the following

<form>     ...     <button type="submit" class="hide"></button> </form>  <script>     ...     $(...).find('[type="submit"]').trigger('click');     ... </script> 

¡it works!

like image 102
iBet7o Avatar answered Sep 22 '22 15:09

iBet7o


You can

  • trigger click event on a submit
  • check validation manually with $("form")[0].checkValidity()
  • find invalid elements manually using $("form :invalid")
like image 23
pozs Avatar answered Sep 18 '22 15:09

pozs