const chineseReg = /[\u3000\u3400-\u4DBF\u4E00-\u9FFF]/ ; export interface ValidatorConfig { minLength : number ; maxLength : number ; noSpace : boolean ; noChinese : boolean ; } export function validate ( text , config ) { if ( config . minLength !== undefined ) { if ( text . length < config . minLength ) { return { result : false , reason : "长度过短" , } ; } } if ( config . maxLength !== undefined ) { if ( text . length > config . maxLength ) { return { result : false , reason : "长度超出" , } ; } } if ( config . noSpace && text . includes ( " " ) ) { return { result : false , reason : "不应含有空格" , } ; } if ( config . noChinese && chineseReg . test ( text ) ) { return { result : false , reason : "不应含有中文字符" , } ; } return { result : true , } ; }