Vue.js 如何在vee validate 3.0版本中验证十进制值

Vue.js 如何在vee validate 3.0版本中验证十进制值,vue.js,vuejs2,Vue.js,Vuejs2,我想验证纬度和逻辑度值,我的代码是 <ValidationProvider :name=waypointLang.form.latitude rules="required|decimal"> <div slot-scope="{ errors }"> <input v-model="waypoint.latitude" readonly name="latitude" type="text" autofocus :p

我想验证纬度和逻辑度值,我的代码是

    <ValidationProvider :name=waypointLang.form.latitude rules="required|decimal">
        <div slot-scope="{ errors }">
            <input v-model="waypoint.latitude" readonly name="latitude" type="text" autofocus :placeholder="waypointLang.form.latitude" class="form-control" id="latitude"/>
                   <span class="required-field">{{ errors[0]}}</span>
        </div>
    </ValidationProvider>

{{错误[0]}
我得到一个错误,没有这样的验证器'decimal'存在


提前感谢

您可以通过将此添加到./plugins/vee-validate.js文件来添加自定义规则:

extend(“十进制”{
验证:(值,{小数='*',分隔符='.}={})=>{
如果(值===null | |值===undefined | |值====''){
返回{
有效:假
};
}
if(数字(小数)==0){
返回{
有效:/^-?\d*$/.测试(值),
};
}
常量regexPart=decimals=='*'?'+':`{1,${decimals}}`;
const regex=new RegExp(`^[-+]?\\d*(\\${separator}\\d${regexPart})?([eE]{1}[-]?\\d+)?$`);
返回{
有效:正则表达式测试(值),
};
},
消息:“{u字段{}字段只能包含十进制值”

})
如果需要支持较大的数字和指数,也可以使用BigNumber.js解决此问题:

Validator.extend('decimals', {
    validate(value, args) {
        const decimals = new BigNumber(value).decimalPlaces()

        if (args[0] === null || args[0] === undefined) {
            return decimals >= 0
        }

        return decimals <= args[0]
    }
})
Validator.extend('decimals'{
验证(值,参数){
常量小数=新的大数(值).decimalPlaces()
if(args[0]==null | | args[0]==未定义){
返回小数>=0
}

返回小数谢谢你的回答。这非常有用。如果这也验证了小数点的数量,那就太好了。