42

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

4
  • If all your users are using Chrome you can use the checkValidity method. Example - too bad IE is still faaar behind. Commented Jul 8, 2012 at 7:33
  • Why are you using a link, not a submit button? Commented Jul 8, 2012 at 10:37
  • 2
    'My point is if there is a way to force the jquery .submit() to use html5 required atribute.'. His form is most likely just to illustrate the issue without further complicating the issue. Commented Jan 5, 2013 at 19:28
  • Good question and good try Commented May 17, 2015 at 1:25

9 Answers 9

46

I did the following

<form>
    ...
    <button type="submit" class="hide"></button>
</form>

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

¡it works!

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

1 Comment

class="hide" or style="display:none"
39

You can

  • trigger click event on a submit
  • check validation manually with $("form")[0].checkValidity()
  • find invalid elements manually using $("form :invalid")

Comments

6

Here an exemple of my personnal solution:

var form = $('#loginform');

// Trigger HTML5 validity.
var reportValidity = form[0].reportValidity();

// Then submit if form is OK.
if(reportValidity){
    form.submit();
}

Comments

2

You are right. HTML5 validation works only when submit is triggered by user. this is how it is. :(

you need write a your own custom method on submit. OR there is a good plugin you can use which validates fields according html5 attributes.

here is the plugin link

Comments

2

Answer from https://stackoverflow.com/a/30124479/7915308

<!DOCTYPE html>
<html>
<body>

<form name="testform" action="action_page.php">
E-mail: <input type="email" name="email" required><br/>
<a href="#" onclick="javascript:console.log(document.testform.reportValidity());return false;">Report validity</a><br/>
<a href="#" onclick="javascript:console.log(document.testform.checkValidity());return false;">Check validity</a><br/>
<a href="#" onclick="javascript:if(document.testform.reportValidity()){document.testform.submit();}return false;">Submit</a>
</form>

</body>
</html>

Comments

1

I had exactly the same problem. I had a single button that I had deliberately set NOT to be a submit because it was on first click revealing a form. On a second click of the same button $('form[name=xxx]').submit(); was firing and I found as well that the HTML5 validation doesn't work.

My trick was to add a line of code so that after my first click of this button occurs I change its type to submit dynamically.

$("#btn").attr("type","submit");

Which worked like a charm!

Comments

0

You could call a function instead and use that function to check if all the nodes with the required attribute have a value.

There are some plugins available to do this: http://thechangelog.com/post/1536000767/h5validate-html5-form-validation-for-jquery

2 Comments

im aware of that, Im trying to use jquery and html5 together, as I want to be html5 my main validator
There are some plugins available.
0

Here are some examples, how to validate html5:

function validateForm(form) {
  if (form.username.value=='') {
    alert('Username missing');
    return false;
  }
  return true;
}
<form action="index.php" name="loginform" method="post" onsubmit="if (!validateForm(this)) event.preventDefault();">
  <input name="username" placeholder="username" required="required" type="text" />
  <a href="" onclick="event.preventDefault(); $(document.forms.loginform).submit();">Login1</a>
  <a style="cursor:pointer;" onclick="$(document.forms.loginform).submit();">Login2</a>
  <input name="send" type="submit" value="Login3" />
  <input name="send" type="button" value="Login4" onclick="$(document.forms.loginform).submit();" />
</form>

Full code here. One thing here is strange: $(document.forms.loginform).submit(); works fine, but document.forms.loginform.submit(); fails. Can somebody explain why?

Comments

-1

Working demo http://jsfiddle.net/3gFmC/

Hope this will help :)

code

$('#btn_submit').bind('click', function() {

        $('input:text[required]').parent().show();
        // do whatever;
 });

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.