当前位置: 首页 > Web前端 > JavaScript

前端表单验证常用的25个JS正则表达式

时间:2023-03-27 12:42:29 JavaScript

1.手机号验证constphoneReg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/constphoneStr1='18886233487'console.log(phoneReg.test(phoneStr1))//trueconstphoneStr2='17283017203897'console.log(phoneReg.test(phoneStr2))//false2,验证身份证constsfzReg=/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/constsfzStr1='415106199801012130'console.log(sfzReg.test(sfzStr1))//trueconstsfzStr2='718381298381212183'console.log(sfzReg.test(sfzStr2))//false3、邮箱验证constemailReg=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/constemailStrWY='956666@163.com'//163邮箱constemailStrQQ='956666@qq.com'//qq邮箱控制台。log(emailReg.test(emailStrWY))//trueconsole.log(emailReg.test(emailStrQQ))//trueconstnoEmail='72873213.com'console.log(emailReg.test(noEmail))//false4,证明URL测试consturlReg=/^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)*\/?$/consturlStr1='https://haha.sunshine.com/xxx/xxx'console.log(urlReg.test(urlStr1))//trueconsturlStr2='sss://haha.sunshine.com/xxx/xxx'console.log(urlReg.test(urlStr2))//false5、IPv4的实验constipv4Reg=/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/constipv4Str1='122.12.56.65'console.log(ipv4Reg.test(ipv4Str1))//trueconstipv4Str2='122.12.56.655'console.log(ipv4Reg.test(ipv4Str2))//false6、16进制颜色的校试constcolor16Reg=/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/constcolor16Str1='#fff'console.log(color16Reg.test(color16Str1))//trueconstcolor16Str2='#1234567'console.log(color16Reg.test(color16Str2))//false7、日期YYYY-MM-DDconstdateReg=/^\d{4}(\-)\d{1,2}\1\d{1,2}$/constdateStr1='2021-10-10'console.log(dateReg.test(dateStr1))//trueconstdateStr2='2021-01-011'console.log(dateReg.test(dateStr2))//false8、日期YYYY-MM-DDhh:mm:ssconstdateReg=/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})(\d{1,2}):(\d{1,2}):(\d{1,2})$/constdateStr1='2021-10-1016:16:16'console.log(dateReg.test(dateStr1))//trueconstdateStr2='2021-10-1016:'console.log(dateReg.test(dateStr2))//false9,整数验证constintReg=/^[-+]?\d*$/constintNum1=12345console.log(intReg.test(intNum1))//trueconstintNum2=12345.1console.log(intReg.test(intNum2))//false10,十进制验证constfloatReg=/^[-\+]?\d+(\.\d+)?$/constfloatNum=1234.5console.log(floatReg.test(floatNum))//true11,保留n小数位functioncheckFloat(n){returnnewRegExp(`^([1-9]+[\d]*(.[0-9]{1,${n}})?)$`)}//保留2位小数constfloatReg=checkFloat(2)constfloatNum1=1234.5console.log(floatReg.test(floatNum1))//trueconstfloatNum2=1234.55console.log(floatReg.test(floatNum2))//trueconstfloatNum3=1234.555console.log(floatReg.test(floatNum3))//false12,邮政编码验证constpostalNoReg=/^\d{6}$/constpostalNoStr1='522000'console.log(postalNoReg.test(postalNoStr1))//trueconstpostalNoStr2='5220000'缺点ole.log(postalNoReg.test(postalNoStr2))//false13,QQ号验证(5-11位)constqqReg=/^[1-9][0-9]{4,10}$/constqqStr1='1915801633'console.log(qqReg.test(qqStr1))//trueconstqqStr2='191580163333'console.log(qqReg.test(qqStr2))//false14,微信号验证(6到20位数字,以字母、字母、数字、减号、下划线开头)constwxReg=/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/constwxStr1='linsanxin885577'console.log(wxReg.test(wxStr1))//trueconstwxStr2='Amazingmyvx'console.log(wxReg.test(wxStr2))//false15,车牌号校验constcarNoReg=/^[北京、天津、上海、重庆、河北、河南、云辽、黑乡、安徽、鲁新、江浙赣湖北、甘肃、山西、蒙古、山西、福建、贵州、广东、青海、西藏、四川、宁琼、使节A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9港澳警员]{1}$/constcarNoStr1='粤A12345'console.log(carNoReg.test(carNoStr1))//trueconstcarNoStr2='粤A12345'console.log(carNoReg.test(carNoStr2))//false16,一个只包含字母的字符串constletterReg=/^[a-zA-Z]+$/constletterStr1='sunshineLin'console.log(letterReg.test(letterStr1))//trueconstletterStr2='sunshine_Lin'console.log(letterReg.test(letterStr2))//false17,包含中文的字符串constcnReg=/[\u4E00-\u9FA5]/constcnStr1='我是sunshine_Lin,林三鑫'console.log(cnReg.test(cnStr1))//trueconstcnStr2='sunshine_Lin'console.log(cnReg.test(cnStr2))//false18,密码强度验证(密码必须包含字母、数字、特殊字符,最少8个字符,最多30个字符)constpasswordReg=/(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}/constpassword1='sunshine_Lin12345..'console.log(passwordReg.test(password1))//trueconstpassword2='sunshineLin12345'console.log(passwordReg.test(password2))//false19,字符串长度n的校验函数checkStrLength(n){returnnewRegExp(`^.{${n}}$`)}//检查长度为3的字符串constlengthReg=checkStrLength(3)conststr1='hhh'console.log(lengthReg.test(str1))//trueconststr2='hhhhh'console.log(lengthReg.test(str2))//false20,文件扩展名校验函数checkFileName(arr){arr=arr.map(name=>`.${name}`).join('|')returnnewRegExp(`(${arr})$`)}constfilenameReg=checkFileName(['jpg','png','txt'])constfilename1='sunshine.jpg'console.log(filenameReg.test(filename1))//trueconstfilename2='sunshine.png'console.log(filenameReg.test(filename2))//trueconst文件name3='sunshine.txt'console.log(filenameReg.test(filename3))//trueconstfilename4='sunshine.md'console.log(filenameReg.test(filename4))//false21,匹配img和srcconstimgReg=//igconsthtmlStr='

'console.log(imgReg.exec(htmlStr))//[//'',//'sunshine.png',//索引:11,//输入:'
',//groups:undefined//]console.log(imgReg.exec(htmlStr))//[//'',//'sunshine111.png',//索引:37,//input:'
',//groups:undefined//]22.在html中匹配注释constnoteReg=//gconsthtmlStr='
<div>
'console.log(noteReg.exec(htmlStr))//[//'',//'adivtag',//index:0,//input:'
',//groups:undefined//]console.log(noteReg.exec(htmlStr))//[//'',//'adivtag',//index:27,//input:'
',//groups:undefined//]23.匹配htmlstyleReg中的styleconst=/([(\s+\w+=)|>])/gconsthtmlStr='
'console.log(styleReg.exec(htmlStr))//[//'>',//'>',//索引:5,//输入:'
',//组:未定义//]console.log(styleReg.exec(htmlStr))//[//'>',//'>',//索引:36,//输入:'
',//组:未定义//]24。匹配html中的颜色constcolorReg=/#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/gconsthtmlStr='
'console.log(colorReg.exec(htmlStr))//[//'#000',//'000',//索引:23,//输入:'
',//groups:undefined//]console.log(colorReg.exec(htmlStr))//[//'#fff',//'fff',//索引:49,//输入:'
',//groups:undefined//]25、匹配htmlTag(html标签)constendReg=/<("[^"]*"|'[^']*'|[^'">])*>/gconsthtmlStr='

'console.log(endReg.exec(htmlStr))

猜你喜欢