[whatwg] Three concerns regarding HTML5 form validation

Ian Hickson ian at hixie.ch
Thu Jan 26 14:38:19 PST 2012

On Mon, 26 Sep 2011, Matias wrote:
> I don't know if this can/should be specified in the spec or if it's up 
> to the browser implementors but here's some concerns I have regarding 
> HTML5 form validation;
> 1) Is there a reason why native form validation seems to be tied to the 
> click of the submit button? Submitting the form using JavaScript does 
> not seem to trigger HTML5 form validation in either Firefox, Opera or 
> the Webkit browsers (I haven't checked how IE10 behaves yet). To me this 
> seems like an unnecessary limitation.

It's intentional, to allow the script to decide whether or not to perform 
the validation. You can use form.checkValidity() to test for validity; it 
causes an 'invalid' event to be fired at all the form controls that are 

> 2) How about adding a method to programmatically display the native 
> validation error messages instead of waiting for the form submit click? 
> We already have a way to (more or less) detect if the form is valid or 
> not, but no way of forcing the native validation error messages to 
> render without using some submit button click hack.

The theory is that if you are not letting the user agent submit the form, 
you probably also want to display the validation UI manually. You can 
easily do that by using the "validationMessage" attribute on all the 
elements in the form.elements list.

I would be interested in studying pages that do JS-based submission but 
still want to do UA-based validation; if you have any pages where you'd 
like to do that, please do send us links!

> 3) Should there be a way of detecting if the browser actually understood 
> all input element types? Browsers believe that a form is valid even when 
> it contains invalid data for input types the browser doesn't understand. 
> (Like the string 'asdf' in an input type=number field in Firefox.)

You can test using (input.type == input.getAttribute('type')).

Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

More information about the whatwg mailing list