Format number in an HTML5 input tag

后端 未结 3 1517
星月不相逢
星月不相逢 2021-01-18 17:00

This is my input


Is it possible to only allow users to enter

3条回答
  •  逝去的感伤
    2021-01-18 17:34

    It can be achieved using javascript with corner case handling using toLocaleString()

    function getChange(){
      // 48 - 57 (0-9)
      var str1 = valueRef.value;
      if((str1[str1.length-1]).charCodeAt() < 48 || (str1[str1.length-1]).charCodeAt() > 57){
        valueRef.value = str1.substring(0,str1.length-1 );
        return;
      } 
    
     // t.replace(/,/g,'')
      let str = (valueRef.value).replace(/,/g,'');
    
      let value = +str;
    
      valueRef.value = value.toLocaleString();
    }
    

    Working Codepen Demo Link

提交回复
热议问题