Input number validation - Restrict to enter only numbers or digits, int & float both

前端 未结 6 2051
迷失自我
迷失自我 2020-12-03 03:50

How to restrict the input field to enter only numbers/digits int and float both. Sometimes we need to allow both integer as well as float value for fields like amount, so in

6条回答
  •  离开以前
    2020-12-03 04:53

    One-liner solution #1:

    Use with the step attribute.

     // The user can enter up to 4 decimal places.
    
     // Not supported in all browsers, but permits numbers of any decimal precision.
    

    You can also use the min and/or max attributes to set the minimum value and/or the maximum value.


    One-liner solution #2:

    Use a regular text input element with a RegExp pattern attribute.

    
    

    This RegExp accepts numbers beginning with a dot ( . ) and/or a negative sign ( - ).

提交回复
热议问题