Thursday 15 August 2013

javascript - How to get the raw value an field? -



javascript - How to get the raw value an <input type="number"> field? -

how can "real" value of <input type="number"> field?

i have input box, , i'm using newer html5 input type number:

<input id="edquantity" type="number">

this supported in chrome 29:

what need ability read "raw" value user has entered in input box. if user has entered number:

then edquantity.value = 4, , well.

but if user enters invalid text, want color input-box red:

unfortunately, type="number" input box, if value in text-box not number value returns empty string:

edquantity.value = "" (string);

(in chrome 29 @ least)

how can "raw" value of <input type="number"> control?

i tried looking through chrome's list of other properties of input box:

i didn't see resembles actual input.

nor find way tell if box "is empty", or not. maybe have inferred:

value isempty conclusion ============= ============= ================ "4" false valid number "" true empty box; not problem "" false invalid text; color reddish

note: can ignore after horizontal rule; it's filler justify question. also: don't confuse illustration question. people might want reply question reasons other coloring box reddish (one example: converting text "four" latin "4" symbol during onblur event)

how can "raw" value of <input type="number"> control?

bonus reading jsfiddle of of above (updated) quirkmode.org entry new input types

according whatwg, shouldn't able value unless it's valid numeric input. input number field's sanitization algorithm says browser supposed set value empty string if input isn't valid floating point number.

the value sanitization algorithm follows: if value of element not valid floating-point number, set empty string instead.

by specifying type (<input type="number">) you're asking browser work you. if, on other hand, you'd able capture non-numeric input , it, you'd have rely on old tried , true text input field , parse content yourself.

the w3 has same specs , adds:

user agents must not allow user set value non-empty string not valid floating-point number.

javascript html5 validation input

No comments:

Post a Comment