How to handle floats and decimal separators with html5 input type number

后端 未结 10 768
情话喂你
情话喂你 2020-11-27 11:15

Im building web app which is mainly for mobile browsers. Im using input fields with number type, so (most) mobile browsers invokes only number keyboard for better user exper

10条回答
  •  渐次进展
    2020-11-27 11:25

    According to w3.org the value attribute of the number input is defined as a floating-point number. The syntax of the floating-point number seems to only accept dots as decimal separators.

    I've listed a few options below that might be helpful to you:

    1. Using the pattern attribute

    With the pattern attribute you can specify the allowed format with a regular expression in a HTML5 compatible way. Here you could specify that the comma character is allowed and a helpful feedback message if the pattern fails.

    
    

    Note: Cross-browser support varies a lot. It may be complete, partial or non-existant..

    2. JavaScript validation

    You could try to bind a simple callback to for example the onchange (and/or blur) event that would either replace the comma or validate all together.

    3. Disable browser validation ##

    Thirdly you could try to use the formnovalidate attribute on the number inputs with the intention of disabling browser validation for that field all together.

    
    

    4. Combination..?

    
    

提交回复
热议问题