24

Reading a website's source code that uses MVC, I struggled trying to understand some of it.

Code snippet of the view:

function submitForm (action) {
  var forms = document.getElementById('form')
  forms.action = action
  if (forms.checkValidity()) {
    forms.submit()
  } else {
    alert('There is still an empty field')
  }
}

I want to execute some code if the form is missing certain inputs.

0

4 Answers 4

29

checkValidity() is a HTML5 method and

When the checkValidity() method is invoked, if the element is a candidate for constraint validation and does not satisfy its constraints, the user agent must fire a simple event named invalid that is cancelable (but in this case has no default action) at the element and return false. Otherwise, it must only return true without doing anything else.

Please learn more about how to use form validation constraints here. There are number of useful methods you can use, customize and even build custom validation methods.

You also can find basic explanation and examples in w3schools. Hope this helps.

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

4 Comments

I downvoted because, I was looking for an explanation of the documentation and explanation about invalid events.
@user31782 - the question did not ask about the events so the explanation answered the question. To learn more about events please take a look at this dom.spec.whatwg.org/#concept-event-fire
To learn more about events please take a look... That's the point, for some people(like me) the official docs is too technical to understand. I usually come to stackoverflow for examples and explanations. IMO Just adding references to other sources is not worth much. I could google that myself. For others it might be useful and hence your answer could be worth a lot.
As I said, the explanation answered the question. It was not asking about how the events work. You can ask your question, not voting down an answer because it did not answer your question 🙄
9

Just a note for my future self and anyone else looking.

console.log(myform.checkValidity()); // return boolean , true if form is valid, false if anything is invalid in the form

console.log(myform.reportValidity()); // will throw error explaining why  `form.checkValidity()` returning false

Comments

2

For Me this works in jQuery

$('#formSection')[0].checkValidity();

Comments

1

just use a required

Example

<input type"text" id="id" required>

if you press the submit button there an alert text saying PLEASE FILL OUT THIS FIELD

6 Comments

sorry for the late response. yes, but can i make some exception ? i mean what that does that code do ? function submitForm(action){ var forms = document.getElementById('form'); forms.action = action; if(forms.checkValidity()){ forms.submit(); }
just put required only on required fields
Ok, i guess it's too early to go to deeper understanding. Thank you for your answer.
This only works if you have a form tag. Which works against hitting REST endpoints, because of all the automatic baggage that comes along with form submission. Because of these two factors, HTML5 validation is pretty broken IMO.
@PRMan Hitting REST endpoints with a form tag seems ok? Just trap the submit event and do your own handling.
|

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.