求大佬指点一下正则动态匹配(现大致知道是需要拼接一下)

新手上路,请多包涵

需求:input框输入需要限制小数点前后位的位数,且位数是外部组件传值确定,(现input框以实现仅可输入一个小数点,第一个数不为0等等,这些不做考虑。),使用正则的方法为replace

阅读 1.3k
1 个回答

可以参考rc-input-number(antd)使用的数字输入框的逻辑。在失焦事件中做如下判断。

// '1.' '1x' 'xx' '' => are not complete numbers
function isNotCompleteNumber(num) {
    return (
        isNaN(num) ||
        num === '' ||
        num === null ||
        (num && num.toString().indexOf('.') === num.toString().length - 1)
    );
}

const newValue = isNotCompleteNumber(parseFloat(inputValue, 10)) ? null : parseFloat(inputValue, 10);
const newValueInString = newValue.toFixed(precision);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题