text 中取正则 来用取到的正则判断val()
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$(".pp").blur(function () {
var txt = $(this).val();
var reg = /^\d{1}$/;
if (!reg.test(txt)) {
$(this).val("");
}
else {
alert(1);
}
});
$(".pd").blur(function () {
var txt = $(this).val();
var val = $(this).attr("rg");
var reg=new RegExp(val);
if (!reg.test(txt)) {
$(this).val("");
}
else {
alert(1);
}
});
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" class="pp"/>
<input type="text" class="pd" rg="\d{1}" /> //错误 需要转义字符
<input type="text" class="pd" rg='/^\\d{1}\\w$/' />
<input type="text" class="pd" rg='/^\\d{2}$/' />
<input type="text" class="pd" rg='/^\\d{3}$/' />
</div>
</form>
</body>
</html>