使 HTML5 input type=“number” 接受破折號
要允許 HTML5 input type = "number" 接受破折號,請使用正則表示式。
如下所示,在 pattern 屬性中新增正則表示式。
[ 0 - 9 ] + ([ - \, ] [0 - 9] + ) ? "
現在將其新增到程式碼中
input type = "text" pattern = "[0-9]+([-\,][0-9]+)?" name = "my-num" title = "dashes or comma"/>
上述方法允許你在數字中新增破折號。但是,上述方法中需要使用 input type 文字才能正常工作。
廣告