views:

166

answers:

1
+4  A: 

The HTML5 draft defines:

The value sanitization algorithm is as follows: If the value of the element is not a valid floating point number, then set it to the empty string instead.

http://www.w3.org/TR/html5/forms.html#number-state

I suppose you'd have use a default value of "0" to make sure wether the field was left untouched or if something invalid was entered, since there seems to be no obvious way to differentiate the two.

EDIT2:

After reading up validation specs and some testing (in Opera 10.54) I concluded that:

<input id="email" type="email" value="blah">
document.getElementById("email").validity.typeMismatch // True

Doesn't work on <input type="number">. Not sure if it's supposed to, or if it's a work in progress. The property does however exist, though it always returns False.

Read more: http://www.w3.org/TR/html5/forms.html#dom-validitystate-typemismatch

You can also set a custom validation method: http://www.w3.org/TR/html5/forms.html#dom-cva-setcustomvalidity

Daniel
Oh balls. Leaving the field blank needs to be a valid state for my purposes (and it's different to entering 0...) Thanks anyway.
nickf
That spec does say "If applying the rules for parsing floating point number values to input results in an error, then return an error; otherwise, return the resulting number." Do you think there'd be any way to catch that error?
nickf
See updated answer.
Daniel