Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HTML form.checkValidity()?

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.

like image 740
Kelpie Avatar asked Aug 21 '17 03:08

Kelpie


3 Answers

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.

like image 116
Navid Avatar answered Oct 17 '22 21:10

Navid


For Me this works in jQuery

$('#formSection')[0].checkValidity();
like image 38
Khushboo Tahir Avatar answered Oct 17 '22 23:10

Khushboo Tahir


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

like image 35
Barclick Flores Velasquez Avatar answered Oct 17 '22 21:10

Barclick Flores Velasquez