首页 > 解决方案 > 大小数的 JavaScript 验证表单

问题描述

I have a form validated by a script, the value of the form cannot be greater than 5. But if you enter 5.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 the form is validated and the value is rounded, how can I do the validation with a large number of decimal places without rounded ?

标签: javascriptvalidationdecimal

解决方案


我通过简单地在点(逗号)之后拆分数字字符串来解决这个问题。在表格中输入极端数据后,小数部分得到验证

function validateY(inp) {
    let elemY = ($('#y_set'))
    let c = (inp.value.replace(',','.'))
    c = c.split('.')
    c = Number(c[1])
    let val = Number((inp.value.replace(',','.')))
    if ( isNaN(val) || typeof(val) != "number" || elemY.val()==="" ){
            return false
    }else if (val === 5 && c > 0)  {
        return false
    }else if (val === -3 && c > 0)  {
        return false
    }
    return val <= 5 && val >= -3
}

c 变量 - (字符串拆分为整数和小数)。5和-3是表格中允许的极值

对不起,如果代码不好,我还在学习


推荐阅读