前端js的正则表达式

参考菜鸟、博客园、csdn

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="javascript:;">
用户名<input type="text" name="usn"><br>
密码<input type="password" name="pwd"><br>
手机号<input type="text" name="phone"><br>
邮箱<input type="text" name="email"><br>
身份证号<input type="text" name="idCard"><br>
<input type="submit" value="提交"><br>
</form>
</body>
<script src="../statics/js/jquery-1.12.4.js"></script>
<script>
$(function () {

//正则表达式验证,使用test方法
$("input[type=submit]").click(function () {

//用户名验证
//用户名正则,4到16位(字母,数字,下划线,减号)
var uPattern = /^[a-zA-Z0-9_-]{4,16}$/;
var usn = $("input[name=usn]").val();

console.log(uPattern.test(usn));

//密码验证
//密码强度正则,最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符
var pPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
//常用密码正则,最少6位,包括至少1个小写字母,1个数字
var pwdPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[a-z])|(?=.*[A-Z]).*$/;
//支付密码正则,必须6位,6个数字
var pwdNumPattern = /^\d{6}$/;
var pwd = $("input[name=pwd]").val();

console.log(pwdNumPattern.test(pwd));
//手机号验证
var phonePattern = /^1[34578]\d{9}$/;
var phone = $("input[name=phone]").val();

console.log(phonePattern.test(phone));
//邮箱验证
//第一种(前端)
var reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
//第二种
var ePattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var email = $("input[name=email]").val();

console.log("邮箱" + reg.test(email));
//身份证号验证
var idPattern = /^[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]$/;
var idCard = $("input[name=idCard]").val();

console.log("身份证号验证" + idPattern.test(idCard));

//验证中文用户名
var zh = /^[\u4e00-\u9fa5]{3,15}$/;
console.log("验证中文用户" + zh.test("蔡根花"));

//车牌号正则
var cPattern = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;

console.log(cPattern.test("粤B39006"));
//微信号正则,6至20位,以字母开头,字母,数字,减号,下划线
var wxPattern = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/;

console.log("微信号正则" + wxPattern.test("caibaojian_com"));

//QQ号正则,5至11位
var qqPattern = /^[1-9][0-9]{4,10}$/;

console.log("QQ号正则" + qqPattern.test("65974040"));

//RGB Hex颜色正则
var cPattern = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;

console.log("RGB" + cPattern.test("#b8b8b8"));

//ipv4地址正则
var ipP = /^(?:(?: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]?)$/;

console.log("ipv4地址正则" + ipP.test("115.28.47.26"));

//URL正则
var urlP= /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;

console.log("URL正则" + urlP.test("http://caibaojian.com"));
})
})
</script>
</html>

posted @ 2019-07-16 13:51  不忆过去,不憧未来!  阅读(1940)  评论(0编辑  收藏  举报