Vue.js Form Validation: Enforcing Numeric Input for newId
You can enforce numeric data type for the 'newId' field using custom validation rules in Vue.js. Here's an example:
const rules = reactive<FormRules>({
newsID: [
{ required: true, message: '请输入关联ID', trigger: 'blur' },
{ max: 30, message: '限制30位字符', trigger: 'blur' },
{
validator: (rule, value, callback) => {
if (typeof value !== 'number') {
callback(new Error('关联ID必须是数字'));
}
else {
callback();
}
},
trigger: 'blur',
},
],
});
This validation rule checks the data type of the 'newId' field. If it's not a number, it displays the error message '关联ID必须是数字'. This ensures that only valid numeric values are accepted for the 'newId' field.
原文地址: https://www.cveoy.top/t/topic/hDbo 著作权归作者所有。请勿转载和采集!