常用正则:数字, 正负数零, 小数点后几位

401 阅读1分钟
限制到小数点后两位
const validate = /^(\-|\+)?\d+(\.\d)?\d{0,1}$/
validate.test(value)

正整数
const validate = /(^[1-9]\d*$)/

验证字符串长度
checkBt(str, max) {
    // 检测字符串长度,max为汉字个数,数字和字母为一个字节,汉字占两个字节
    // eslint-disable-next-line no-control-regex
    const char = str.replace(/[^\x00-\xff]/g, '**')
    const maxlen = max * 2
    return !(char.length > maxlen)
  }
使用: if( !checkBt(value,20) ) 规则没通过提醒最多20个字