Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any JavaScript properties related to web browser form validation in HTML5?

In HTML5, the client side validation should become a job of the web browser, via attributes like pattern or required.

Is there only a CSS implementation of this (i.e. the :valid and :invalid selectors, to give feedback to the user) or is there also a JavaScript implementation?

I’m thinking of something like a feature allowing a JavaScript function to get called if the user clicks submit and the form has invalid values in it. Or a flag that I can access in the form object to show if it has errors or not.

Thanks.

like image 815
SCBoy Avatar asked Nov 04 '10 09:11

SCBoy


People also ask

Can JavaScript be used for form validation?

HTML form validation can be done by JavaScript.

How JavaScript is used in website validation?

JavaScript provides a way to validate form's data on the client's computer before sending it to the web server. Form validation generally performs two functions. Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in.

Does HTML5 have form validation?

Form validation can happen on the client side and the server side. Client side validation occurs using HTML5 attributes and client side JavaScript.

Which is the property given to form to skip the HTML5 validations?

novalidate if a form-level attribute used to turn off validation for a form, despite the attributes of the inputs it contains (i.e. will override inputs with the required attribute, or that would otherwise fail validation).


2 Answers

Yes, there is a validity attribute you can query. See http://dev.w3.org/html5/spec/association-of-controls-and-forms.html#dom-cva-validity

I've no idea how much support for this exists in browsers currently.

like image 156
Alohci Avatar answered Sep 30 '22 00:09

Alohci


Yes, it does, and it currently works. See A List Apart's excellent article on the subject by Ryan Seddon. According to the article, Chrome 4+, Safari 5+ and Opera 9.6+ all support the properties. (He also includes an example.)

Quoting from the article, you can do things like:

input:focus:required:invalid {
  background: pink url(ico_validation.png) 379px 3px no-repeat;
}
input:required:valid {
  background-color: #fff;
  background-position: 379px -61px;
}

And when an input validates it will display one icon, and when it is invalidated and focused it will display another.

like image 34
Sean Vieira Avatar answered Sep 29 '22 23:09

Sean Vieira