Skip to content Skip to sidebar Skip to footer

Using Javascript, How To Set A Value To A Number Input That Ends In Decimal Dot (ie. "100.")

Setup Suppose I have an input like this: As this is a number field, it will accept digits, minus sign and decimal dot. Thu

Solution 1:

Since you're looking to accept non-number values (like "100.") I believe usage of type="number" is too specialized for you. I recommend you use type="text" and pattern, with a custom regex:

input:invalid { background-color: #ffa0a0; }
<inputtype="text"pattern="[1-9][0-9]*([.][0-9]?)?"/>

The pattern I used is:

[1-9][0-9]*([.][0-9]?)?

[1-9]                     any non-0 digit (since the leading digit shouldn't be 0)
     [0-9]*               any number of followup digits
           (         )?   a whole optional section[.]a single "." character
               [0-9]?     an optional single digit

Post a Comment for "Using Javascript, How To Set A Value To A Number Input That Ends In Decimal Dot (ie. "100.")"