前端开发-常用正则校验
1、手机号正则校验 (2019年)
let phoneRule = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
let phoneNum = "17598778921"
phoneRule.test(phoneNum )
/**手机号正则*/ export const phoneRule = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/; /**手机号校验*/ export const phoneFun = (params) => { const regex = new RegExp(phoneRule); return regex.test(params) }
2、身份证正则校验(一代、二代)
let cardIdRule = /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0[1-9]|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/;
cardIdRule.test("411361199603150723")
/**身份证正则*/ export const cardIdRule = /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0[1-9]|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/;
/***身份证校验*/ export const cardIdFun = (params) => { const regex = new RegExp(cardIdRule); return regex.test(params) }
3、英文字母与数字组合校验
let numEnsRule = /^[A-Za-z0-9]+$/;
numEnsRule.test('00aa77')
/**英文和数字正则*/ export const numEnsRule = /^[A-Za-z0-9]+$/; /**英文和数字校验*/ export const numEnsRuleFun = (params) => { const regex = new RegExp(numEnsRule); return regex.test(params) }
4、中文校验-是否含有中文
let ChineseRule = /[\u4e00-\u9fa5]/;
ChineseRule.test('端口号')
/**中文字符正则 */ const ChineseRule = /[\u4e00-\u9fa5]/; /**中文字符校验 */ const ChineseFun = (params) => { const regex = new RegExp(ChineseRule); return regex.test(params) }
5、email邮箱正则校验
let EmailRule = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
EmailRule.test('caoyy@bssfit.cn')
/**邮箱正则 */ const EmailRule = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; /**邮箱校验 */ const EmailFun = (params) => { const regex = new RegExp(EmailRule); return regex.test(params) }
6、中国邮政编码正则验证
let postalRule = /[1-9]\d{5}(?!\d)/ ;
postalRule.test('473000')
/**中国邮政正则 */ const postalRule = /[1-9]\d{5}(?!\d)/ ; /**中国邮政校验 */ const postalFun = (params) => { const regex = new RegExp(postalRule); return regex.test(params) }
7、QQ号正则验证
/**腾讯QQ号正则 */ const QQRule = /[1-9][0-9]{4,}/; /**腾讯QQ号校验 */ const QQFun = (params) => { const regex = new RegExp(QQRule); return regex.test(params) }
8、校验只能由数字,字母,下划线组成,不少于6位:
/^[a-zA-Z\d_]{6,}$/.test(val)

浙公网安备 33010602011771号