Re: <input type="number"> restricting user input to only valid characters

On Tue, 14 Jul 2009, Michael A. Puls II wrote:
> On Tue, 14 Jul 2009 01:29:43 -0400, Ian Hickson <ian@hixie.ch> wrote:
> > On Fri, 19 Jun 2009, Michael A. Puls II wrote:
> > > 
> > > With <input type="number">, if a user enters "abc" for example, the
> > > specs says to handle that as if the value is an empty string.
> > 
> > No, the spec doesn't say how to handle it. That's a user interface issue,
> > out of scope of the spec.
> > 
> > It does say, however, that:
> > 
> > # User agents must not allow the user to set the value to a string that is
> > # not a valid floating point number.
> > 
> > ...so if the user does enter "abc", it won't affect the element's value.
> 
> <http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#number-state>
> I do see "User agents must not allow the user to set the value to a string
> that is not a valid floating point number".
> 
> But, I also see "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.", which is where I got the empty string part from.

What the user enters (or sees!) is not the value. The value is the 
underlying data that is then converted for display, and the data the user 
enters is converted back to the value. The value sanitization algorithm is 
never applied to user input by the spec.


> But, with "User agents must not allow the user to set the value to a 
> string that is not a valid floating point number", what does that mean 
> with the following?
> 
> <input type="number">
> 
> If the user types "abc", should .value return "abc" or "" or "whatever 
> the last valid value was"?

It can return either "" or a valid number. It can never return "abc".


> Or, is "abc" only converted to "" when submitting?

It's converted to "" whenever the value sanitization algorithm is 
invoked, which is only when the element is created or when it changes 
type.

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

Received on Tuesday, 14 July 2009 23:36:58 UTC