Make HTML5 input type=“number” accepting dashes


To allow HTML5 input type = ”number” to accept dashes, use a regular expression.

Add the regular expression in the pattern attribute as shown below.

[ 0 - 9 ] + ([ - \, ] [0 - 9] + ) ? "

Add it to the code now:

input type = "text" pattern = "[0-9]+([-\,][0-9]+)?" name = "my-num" title = "dashes or comma"/>

The above will allow you to add dashes in number. However, above you need to use input type text for the solution to work.

Updated on: 29-Jan-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements