前端开发-常用正则校验
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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!