一百行js代码实现一个验证工具
export default class Coi {
constructor(prop) {
this.input = prop
this.errorMessage = '通过校验' // 错误信息
this.pass = true // 校验是否通过
}
// 数据输入
data(input) {
if (!this.pass) return this
this.input = input
return this
}
// 必填,不能为空
isRequired(message) {
if (!this.pass) return this
if (
/^\s*$/g.test(this.input) ||
this.input === null ||
this.input === undefined
) {
this.errorMessage = message
this.pass = false
}
return this
}
// 最小长度
minLength(length, message) {
if (!this.pass) return this
if (this.input.length < length) {
this.errorMessage = message
this.