1.必填
页面.js(核心代码 非完全)
//引入检测方法
import validate from '../utils/validate.js'//方法:验证填写信息
//填写规则
const rules = {
name: {
rule: /\S/,
info: "*机床名字必填"
},
ip: {
rule: /\S/,
info: "*机床ip必填"
},
equipmentType: {
rule: /\S/,
info: "*设备类型必选"
}
}
//构造函数
constructor(props) {
super(props)
this.state = {
message: { // 填写的数据
boxId: '',
equipmentType: '',
productModel: '',
name: '',
ip: '',
line: '',
},
nameBool: true,//名字填写是都符合规范
ipBool: true,//ip填写是都符合规范
equipmentTypeBool: true//设备类型填写是都符合规范
}
}
// 验证机床名字、IP、设备类型必填
const message = this.state.message
if (!validate.validateNull('equipmentType', rules.equipmentType.rule, message)) {
this.setState({
equipmentTypeBool: false
})
return
}
this.setState({
equipmentTypeBool: true
})
验证方法页面.js
//判断验证是否符合要求(目前时为空) 验证message的key值是否符合rule规范 rule.test(message[key])
function validateNull(key, rule, message) {
if (!rule.test(message[key])) {
return false
}
return true
}