<head runat="server">
<title>javascript验证客户端电子邮件格式</title>
<script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function CheckEmail(strEmail, name) {
if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
alert("您输入的电子邮件格式正确");
return true;
}
else {
alert("您输入的电子邮件格式有误,请重新输入");
document.getElementById(name).focus();
}
}
// onblur 失去焦点时获得的事件
//正确(空或汉字)
function CheckName(strName, name) {
// alert(name);
if (strName.search(/^[\u4e00-\u9fa5]{1,10}$/) != -1) {
alert("您输入的名字正确,不能为空,长度>=1,小于等于10");
return true;
}
else {
alert("只能输入汉字,请重新输入,长度>=1小于等于10");
document.getElementById(name).focus();
}
}
function CheckZipCode(strName, id) {
if (strName.search(/^[0-9]{6}$/) != -1) {
alert("您输入的邮政编码正确,六位数字!");
return true;
}
else {
alert("错误,只能输入六位数字!");
document.getElementById(id).focus();
}
}
function CheckTel(strName, id) {
//^[0-9]{3,4}-?[0-9]{7,8}$
//区号电话^0(([0-9]d)|([3-9]d{2}))d{8}$
//电话[0-9]){7,8}
//手机^1[0-9]{10}$
if (strName.search(/^((0[0-9]{2,3}-?[0-9]{7,8})|([0-9]{7,8})|(1[0-9]{10}))$/) != -1) {
alert("您输入的电话正确,'区号-电话'或手机号,'区号电话''电话'");
return true;
}
else {
alert("错误,区号-电话或手机号!");
document.getElementById(id).focus();
}
}
// function CheckUrl(strName, id) {
// //Url:^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
// var str = "^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?$";
// if (strName.search(/^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?$/) != -1) {
// alert("URL正确");
// return true;
// }
// else {
// alert("错误,正确的url'http://'");
// document.getElementById(id).focus();
// }
// }
function CheckInt(strName, id) {
if (strName.search(/^([1-9]\d{0,3})|0$/) != -1) {
alert("输入整形正确");
return true;
}
else {
alert("wrong,请输入正整数或0");
document.getElementById(id).focus();
}
}
function CheckIntDouble(strName, id) {
//^([1-9]+\.?\d+)|(0\.\d+)$
if (strName.search(/^(-?[1-9]+(\.)?[0-9]{1,4})|(-?0[.][1-9]+)$/) != -1) {
alert("输入正整数或正小数正确");
return true;
}
else {
alert("wrong,请输入正整数或正小数");
document.getElementById(id).focus();
}
}
function CheckDot(strName, id) {
//^([-]?0[.][0-9]+)|([-]?[1-9]+[.]{0,1}[0-9]*
if (strName.search(/^([-]?0[.][0-9]+)$|^([-]?[1-9]+[.]{0,1}[0-9]*)$/) != -1) {
alert("正确,输入'.'");
return true;
}
else {
alert("wrong,输入'.'");
document.getElementById(id).focus();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="MyEmail" runat="server" οnblur="CheckEmail(this.value,this.id)"></asp:TextBox>
<input type="text" id="MyName" οnblur="CheckDot(this.value,this.id)" />
</div>
</form>
</body>
</html>