<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../common/head.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>正则表达式</title>
<style type="text/css">
#no {
color: #FF0000;
}
#ok {
color: #00FF00;
}
</style>
<script type="text/javascript">
function checkName() {
alert("ok");
}
$(function() {
$("tr").each(function(){
$($(this).children("td").get(0)).attr("align","right");
});
$("#name").blur(function() {
var v = $(this).val();
var regex = /^\w{4,20}$/;
if (!regex.exec(v)) {
$("#nameCheck").html("<div id='no'>只能是4-20位字符串!!</div>");
} else {
$("#nameCheck").html("<div id='ok'>名称合法!!</div>");
}
});
$("#birthday").blur(function() {
var v = $(this).val();
var regex = /^(\d{4})(-)(\d{1,2})\2(\d{1,2})$/;
var check = v.match(regex);
if (check == null) {
$("#birthdayCheck").html("<div id='no'>格式不正确,如:2014-12-31</div>");
} else {
var array = v.split("-");
var year = array[0];
var moth = array[1];
var date = array[2];
if(year > 2014 || year < 1949){
$("#birthdayCheck").html("<div id='no'>年份只能在1949-2014之间</div>");
} else if(moth > 12 || moth < 1){
$("#birthdayCheck").html("<div id='no'>月份只能在1-12之间</div>");
} else if(date > 31 || date < 1){
$("#birthdayCheck").html("<div id='no'>日只能在1-31之间</div>");
}else{
$("#birthdayCheck").html("<div id='ok'>出生日期合法</div>");
}
}
});
$("#hanzi").blur(function() {
var v = $(this).val();
var regex = /[^\u4e00-\u9fa5]/;
var check = regex.test(v);
if (check) {
$("#hanziCheck").html("<div id='no'>只能写汉字</div>");
} else {
$("#hanziCheck").html("<div id='ok'>内容正确</div>");
}
});
$("#swkb").blur(function() {
var v = $(this).val();
var regex = /^\s+|\s+$/;
var check = v.match(regex);
// alert(check);
if (check == null) {
$("#swkbCheck").html("<div id='no'>首尾都不是空白</div>");
} else {
$("#swkbCheck").html("<div id='ok'>内容正确</div>");
}
});
$("#mail").blur(function() {
var v = $(this).val();
var regex = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
var check = v.match(regex);
// alert(check);
if (check == null) {
$("#mailCheck").html("<div id='no'>邮箱格式不正确</div>");
} else {
$("#mailCheck").html("<div id='ok'>邮箱正确</div>");
}
});
$("#int").blur(function() {
var v = $(this).val();
var regex = /^-?[1-9]\d*$/;
var check = v.match(regex);
if (check == null) {
$("#intCheck").html("<div id='no'>请输入整数</div>");
} else {
$("#intCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#int_z").blur(function() {
var v = $(this).val();
var regex = /^[1-9]\d*$/;
var check = v.match(regex);
if (check == null) {
$("#int_zCheck").html("<div id='no'>请输入正整数</div>");
} else {
$("#int_zCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#int_f").blur(function() {
var v = $(this).val();
var regex = /^-[1-9]\d*$/;
var check = v.match(regex);
if (check == null) {
$("#int_fCheck").html("<div id='no'>请输入负整数</div>");
} else {
$("#int_fCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#num").blur(function() {
var v = $(this).val();
var regex = /^([+-]?)([1-9]\d*\.?\d+|0\.\d*)$/;
var check = v.match(regex);
if (check == null) {
$("#numCheck").html("<div id='no'>请输入数字</div>");
} else {
$("#numCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#int_z0").blur(function() {
var v = $(this).val();
var regex = /^([1-9]\d*|0)$/;
var check = v.match(regex);
// alert(check);
if (check == null) {
$("#int_z0Check").html("<div id='no'>请输入正整数或0</div>");
} else {
$("#int_z0Check").html("<div id='ok'>输入正确</div>");
}
});
$("#int_f0").blur(function() {
var v = $(this).val();
var regex = /^(-[1-9]\d*|0)$/;
var check = v.match(regex);
if (check == null) {
$("#int_f0Check").html("<div id='no'>请输入负整数或0</div>");
} else {
$("#int_f0Check").html("<div id='ok'>输入正确</div>");
}
});
$("#float").blur(function() {
var v = $(this).val();
var regex = /^([+-]?)([1-9]\d*\.\d+|0\.\d+)$/;
var check = v.match(regex);
if (check == null) {
$("#floatCheck").html("<div id='no'>请输入浮点型数值</div>");
} else {
$("#floatCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#zipcode").blur(function() {
var v = $(this).val();
var regex = /^\d{6}$/;
var check = v.match(regex);
if (check == null) {
$("#zipcodeCheck").html("<div id='no'>请输入邮编</div>");
} else {
$("#zipcodeCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#color").blur(function() {
var v = $(this).val();
var regex = /^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
var check = v.match(regex);
if (check == null) {
$("#colorCheck").html("<div id='no'>请输入三位或六位16进制数</div>");
} else {
$("#colorCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#acsii").blur(function() {
var v = $(this).val();
var regex = /^[\x00-\xFF]+$/;
var check = v.match(regex);
if (check == null) {
$("#acsiiCheck").html("<div id='no'>请输入ACSII</div>");
} else {
$("#acsiiCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#sfz").blur(function() {
var v = $(this).val();
var regex = /^[1-9](\d{14}|\d{17}|\d{16}[xX])$/;
var check = v.match(regex);
if (check == null) {
$("#sfzCheck").html("<div id='no'>请输入正确身份证,十八位或十五位</div>");
} else {
$("#sfzCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#qq").blur(function() {
var v = $(this).val();
var regex = /^[1-9]{1}\d{4,10}$/;
var check = v.match(regex);
if (check == null) {
$("#qqCheck").html("<div id='no'>请输入正确qq</div>");
} else {
$("#qqCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#picture").blur(function() {
var v = $(this).val();
var regex = /^([\w\u4e00-\u9fa5]+)\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$/;
var check = v.match(regex);
if (check == null) {
$("#pictureCheck").html("<div id='no'>请输入正确图片</div>");
} else {
$("#pictureCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#rar").blur(function() {
var v = $(this).val();
var regex = /^([\w\u4e00-\u9fa5]+)\.(rar|zip|7zip|tgz)$/;
var check = v.match(regex);
if (check == null) {
$("#rarCheck").html("<div id='no'>请输入正确压缩文件</div>");
} else {
$("#rarCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#phone").blur(function() {
var v = $(this).val();
var regex = /^(13\d{9}|15[012356789]\d{8}|18[0256789]\d{8}|147\d{8})$/;
var check = v.match(regex);
if (check == null) {
$("#phoneCheck").html("<div id='no'>请输入正确手机号</div>");
} else {
$("#phoneCheck").html("<div id='ok'>输入正确</div>");
}
});
$("#url").blur(function() {
var v = $(this).val();
var regex = /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-\.\/\?%&=]*)?$/;
var check = v.match(regex);
if (check == null) {
$("#urlCheck").html("<div id='no'>请输入正确url</div>");
} else {
$("#urlCheck").html("<div id='ok'>输入正确</div>");
}
});
});
</script>
</head>
<body>
<form action="">
<table align="center">
<tr>
<td width="200px">用户名:</td>
<td width="100px"><input id="name" type="text"></td>
<td width="250px"><div id="nameCheck"></div></td>
</tr>
<tr>
<td>匹配日期:</td>
<td><input id="birthday" type="text"></td>
<td><div id="birthdayCheck"></div></td>
</tr>
<tr>
<td>匹配汉字:</td>
<td><input id="hanzi" type="text"></td>
<td><div id="hanziCheck"></div></td>
</tr>
<tr>
<td>匹配首尾空白:</td>
<td><input id="swkb" type="text"></td>
<td><div id="swkbCheck"></div></td>
</tr>
<tr>
<td>匹配邮箱:</td>
<td><input id="mail" type="text"></td>
<td><div id="mailCheck"></div></td>
</tr>
<tr>
<td>整数:</td>
<td><input id="int" type="text"></td>
<td><div id="intCheck"></div></td>
</tr>
<tr>
<td>正整数:</td>
<td><input id="int_z" type="text"></td>
<td><div id="int_zCheck"></div></td>
</tr>
<tr>
<td>负整数:</td>
<td><input id="int_f" type="text"></td>
<td><div id="int_fCheck"></div></td>
</tr>
<tr>
<td>数字:</td>
<td><input id="num" type="text"></td>
<td><div id="numCheck"></div></td>
</tr>
<tr>
<td>正整数和0:</td>
<td><input id="int_z0" type="text"></td>
<td><div id="int_z0Check"></div></td>
</tr>
<tr>
<td>负整数和0:</td>
<td><input id="int_f0" type="text"></td>
<td><div id="int_f0Check"></div></td>
</tr>
<tr>
<td>浮点数:</td>
<td><input id="float" type="text"></td>
<td><div id="floatCheck"></div></td>
</tr>
<tr>
<td>邮编:</td>
<td><input id="zipcode" type="text"></td>
<td><div id="zipcodeCheck"></div></td>
</tr>
<tr>
<td>颜色:</td>
<td><input id="color" type="text"></td>
<td><div id="colorCheck"></div></td>
</tr>
<tr>
<td>ACSII:</td>
<td><input id="acsii" type="text"></td>
<td><div id="acsiiCheck"></div></td>
</tr>
<tr>
<td>身份证:</td>
<td><input id="sfz" type="text"></td>
<td><div id="sfzCheck"></div></td>
</tr>
<tr>
<td>QQ:</td>
<td><input id="qq" type="text"></td>
<td><div id="qqCheck"></div></td>
</tr>
<tr>
<td>图片:</td>
<td><input id="picture" type="text"></td>
<td><div id="pictureCheck"></div></td>
</tr>
<tr>
<td>压缩文件:</td>
<td><input id="rar" type="text"></td>
<td><div id="rarCheck"></div></td>
</tr>
<tr>
<td>手机:</td>
<td><input id="phone" type="text"></td>
<td><div id="phoneCheck"></div></td>
</tr>
<tr>
<td>url:</td>
<td><input id="url" type="text"></td>
<td><div id="urlCheck"></div></td>
</tr>
</table>
</form>
</body>
</html>
js正则表达式总结
最新推荐文章于 2024-11-07 16:16:00 发布