19

I'm writing form and adding html5 validation attributes to its input like "required", "autofocus". I use Javascript to submit the form using document.myForm.submit() but it doesn't validate the form against the html5 validation attributes as they aren't there.

Any Suggestions?

3 Answers 3

29

It appears that triggering a click on the submit button does have the correct effect: http://jsfiddle.net/e6Hf7/.

document.myForm.submitButton.click();

and in case you don't have a submit button, add an invisible one like:

<input type="submit" style="display:none" name="submitButton">
Sign up to request clarification or add additional context in comments.

12 Comments

@Ahmed Elmorsy: It does for me in Chrome. Does the fiddle fail for you?
I'm also working on Chrome. the property submitButton is undefined
@Ahmed Elmorsy: Obviously, you'd need to name the submit button. In jQuery you could also do $('form > input[type=submit]').click().
I have no submit button I'm making an anchor with href="javascript:document.myForm.submit()"
@Ahmed Elmorsy: You might want to add an invisible one. Like <input type=submit style=display:none>.
|
19

The pimvdb's answer is based on a workaround to include a hidden submit button.

HTML5 provides javascript functions to achieve the same thing without a submit button. As Tigraine pointed out, Mozilla documents two validity checking methods for 'form' elements:

  • checkValidity() return true/false and doesn't show anything to the end user
  • reportValidity() return true/false and ALSO shows the validation messages to the end user (like the normal submit button click does)

<!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

3

Why not simply call the validation manually before you do document.myForm.submit()

What validation framework do you use and what AJAX library?

In case you use jQuery here is the code to prevent the submit:

$('#myForm').submit(function(evt) {
  if (! $('#myForm').validate()) {
    evt.preventDefault();
  }
});

And trigger the submit through:

$('#myForm').submit();

This would call the validation whenever submit is triggered.. And if the validation fails it prevents the submit from executing. But I'd look at your validationframework as it usually should do this already

In case you don't use any JavaScript framework you may want to have a look at: element.checkValidity(); and how to invoke the HTML5 validation from JavaScript before even calling submit.

5 Comments

The "framework" is plain HTML5 and he wants to invoke it's built-in validation functionality.
Thanks.. I was not aware that the validation stuff is already implemented in browsers.. I updated the answer with a pointer to MDN
Yes as Xion said It's plain HTML5 do anybody know how can I use its built-in validation functionality??
oops do this mean that I need to call checkValidity() for each input in the form ?!!
I guess not .. I have no experience with this API so I thought the checkValidity() applies to the whole form.. Otherwise you'd have to traverse the DOM beneath the form.. But in any case it's not a really good solution

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.