1

If you have a <form> and a <button type='submit'> and you click on the submit button, it will do the default form validation, such as checking whether an <input> is required or not. It would normally say Please fill out this field.

However, if I programmatically submit the form through $("form").submit() for example, it would submit it without performing any checks.

Is there a simpler way to perform the default form validations using native JavaScript? There seems to be only checkValidity() on the form element which return true or false. And if I call the same native function on the input itself, it doesn't really do anything.

Here is a demo code of what I mean: http://jsfiddle.net/totszwai/yb7arnda/

2 Answers 2

2

For those still struggling:

You can use the Constraint validation API - https://developer.mozilla.org/en-US/docs/Web/API/Constraint_validation

<div id="app">
  <form>
    <input type="text" required placeholder="name">
    <input type="text" required placeholder="email">
  </form>
  <button id="save">Submit</button>
</div>
const form = document.querySelector("form");
document.getElementById("save").addEventListener("click", e => {
  e.preventDefault();
  if (form.checkValidity()) {
    console.log("submit ...");
  } else {
    form.reportValidity();
  }
});

Check out and play here: https://stackblitz.com/edit/js-t1vhdn?file=index.js

I hope it helps or gives you ideas. :)

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

2 Comments

LOL 5 years later. XD But yeah, I guess hooking up on an event listener on the click would work.
:D LOL - yeah - I'm a turtle. Anyway, I used the button and a click event, but the code could be used in literally any event - what you need would be the form reference so you can check and report ;)
0

I think this might be the answer you are looking for :

JavaScript :

document
.getElementById('button')
.addEventListener("click",function(e) {
     document.getElementById('myForm').validate();
});

HTML :

<form  id="myForm" >
First name: <input type="text" name="FirstName"  required><br>
Last name: <input type="text" name="LastName" required><br>
<button id="button">Trigger Form Submit</button>
</form>

Demo : http://jsfiddle.net/2ahLcd4d/2/

9 Comments

OP did not ask for jQuery.
@NotoriousPet0 Changed to JavaScript from jQuery.
What if i don't have a button in my form? (which is the reason I ask the question) Are you saying that we can't trigger the behavior performed by the button separately?
@codenamezero whenever you want to submit the form, just execute this document.getElementById('#myForm').validate();
Yeah, but I always get TypeError: $(…).validate is not a function. I've already search and tried a couple of potential solutions before I ask the question on stackoverflow. It seems that native function only has checkValidity from the form element and nothing else. And I think validate is a jQuery plugin.
|

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.