0

如果有人尝试输入任何字母,我想限制用户仅输入数字,将使用以下函数删除:

export const digitOnly = (value) => {
try {
    if (!value) {
        return value
    }
    const onlyNums = value.toString().replace(/[^\d]/g, "");
    console.log(onlyNums);
    return onlyNums;
} catch (error) {
    console.error(error);
}

}

在我的表格中,我添加了如下字段:

<Field 
                        name="otp" 
                        type="text"
                        component={loginProcess.renderField} 
                        label="Enter OTP*"
                        normalize={loginProcess.normalizePhone}
                        className="user_name"
                        label="OTP"                            
                        errorLabel="OTP"
                    />

问题是规范化没有反映字段中的值。但是如果我改变这一行 const onlyNums = value.toString().replace(/[^\d]/g, ""); to const onlyNums = value.toString().replace(/[^\d]/g, "-"); 它将字母替换为 -

为什么会这样?

4

0 回答 0