// ******************************************************************
// This function accepts a string variable and verifies if it is in
// the proper format for an e-mail address or not. // The function returns true if the format is valid, false if not.
// ****************************************************************** function isEmail(email) {
invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;"; // Check for null
if (email == "") {
return true;
} // Check for invalid characters as defined above
for (i=0; i<invalidChars.length; i++) {
badChar = invalidChars.charAt(i);
if (email.indexOf(badChar,0) > -1) {
return false;
}
}
lengthOfEmail = email.length;
if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
return false;
}
Pos = email.indexOf("@",1);
if (email.charAt(Pos + 1) == ".") {
return false;
}
while ((Pos < lengthOfEmail) && ( Pos != -1)) {
Pos = email.indexOf(".",Pos);
if (email.charAt(Pos + 1) == ".") {
return false;
}
if (Pos != -1) {
Pos++;
}
} // There must be at least one @ symbol
atPos = email.indexOf("@",1);
if (atPos == -1) {
return false;
} // But only ONE @ symbol
if (email.indexOf("@",atPos+1) != -1) {
return false;
} // Also check for at least one period after the @ symbol
periodPos = email.indexOf(".",atPos);
if (periodPos == -1) {
return false;
}
if (periodPos+3 > email.length) {
return false;
}
return true;
}
// This function accepts a string variable and verifies if it is in
// the proper format for an e-mail address or not. // The function returns true if the format is valid, false if not.
// ****************************************************************** function isEmail(email) {
invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;"; // Check for null
if (email == "") {
return true;
} // Check for invalid characters as defined above
for (i=0; i<invalidChars.length; i++) {
badChar = invalidChars.charAt(i);
if (email.indexOf(badChar,0) > -1) {
return false;
}
}
lengthOfEmail = email.length;
if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
return false;
}
Pos = email.indexOf("@",1);
if (email.charAt(Pos + 1) == ".") {
return false;
}
while ((Pos < lengthOfEmail) && ( Pos != -1)) {
Pos = email.indexOf(".",Pos);
if (email.charAt(Pos + 1) == ".") {
return false;
}
if (Pos != -1) {
Pos++;
}
} // There must be at least one @ symbol
atPos = email.indexOf("@",1);
if (atPos == -1) {
return false;
} // But only ONE @ symbol
if (email.indexOf("@",atPos+1) != -1) {
return false;
} // Also check for at least one period after the @ symbol
periodPos = email.indexOf(".",atPos);
if (periodPos == -1) {
return false;
}
if (periodPos+3 > email.length) {
return false;
}
return true;
}
<!--
alert(/[^\d{15}$|^\d{18$}]/.test("123456789012345"))
function strEmail(str){
var pattern = /^([\.a-zA-Z0-9_-]){2,}@([a-zA-Z0-9_-]){2,}(\.([a-zA-Z0-9]){2,4}){1,2}$/;
return pattern.test(str);
}
//-->
</script>
{
return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(mail));
}
alert(ismail("[email protected]"));
alert(ismail("aaa$bbb.ccc"));
alert(ismail("aaa@bbb"));
<SCRIPT LANGUAGE="JavaScript">
function isIdCardNo(num)
{
if (isNaN(num)) {alert("输入的不是数字!"); return false;}
var len = num.length, re;
if (len == 15)
re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
else if (len == 18)
re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d)$/);
else {alert("输入的数字位数不对!"); return false;}
var a = num.match(re);
if (a != null)
{
if (len==15)
{
var D = new Date("19"+a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];
}
else
{
var D = new Date(a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getFullYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];
}
if (!B) {alert("输入的身份证号 "+ a[0] +" 里出生日期不对!"); return false;}
}
return true;
}
isIdCardNo("330103780133103");
isIdCardNo("330103780123103");
isIdCardNo("330103197801231030");
</SCRIPT>
{
alert('email不正确');
}
function validateEmail() {
var mail = document.forms[0].email.value;
var strr;
re=/(\w+[A-Za-z0-9_.]*@\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i;
re.exec(mail);
if (RegExp.$3!=""&&RegExp.$3!="."&&RegExp.$2!=".") strr=RegExp.$1+RegExp.$2+RegExp.$3
else if (RegExp.$2!=""&&RegExp.$2!=".") strr=RegExp.$1+RegExp.$2
else strr=RegExp.$1
if (strr!=mail) {
alert("Wrong!");
} else {
alert("Correct");
}
}测试身份证(对18位的还测试了校验位):
function validate() { var factorArr = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1); var error;
var varArray = new Array();
var intValue;
var lngProduct = 0;
var intCheckDigit;
var intStrLen = document.checkdigit.idNo.value.length;
var idNumber = document.checkdigit.idNo.value;
// initialize
if ((intStrLen != 15) && (intStrLen != 18)) {
error = "Invalid length.";
alert(error);
document.checkdigit.idNo.focus();
return false;
}
// check and set value
for(i=0;i<intStrLen;i++) {
varArray[i] = idNumber.charAt(i);
if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
error = "Invalid ID Card Number.";
alert(error);
document.checkdigit.idNo.focus();
return false;
} else if (i < 17) {
varArray[i] = varArray[i]*factorArr[i];
}
}
if (intStrLen == 18) {
//check date
var date8 = idNumber.substring(6,14);
if (checkDate(date8) == false) {
error = "Date Info Incorrect.";
alert(error);
return false;
}
// calculate the sum of the products
for(i=0;i<17;i++) {
lngProduct = lngProduct + varArray[i];
}
// calculate the check digit
intCheckDigit = 12 - lngProduct % 11;
switch (intCheckDigit) {
case 10:
intCheckDigit = 'X';
break;
case 11:
intCheckDigit = 0;
break;
case 12:
intCheckDigit = 1;
break;
}
// check last digit
if (varArray[17].toUpperCase() != intCheckDigit) {
error = "Invalid Check Digit. The Correct One Should Be " + intCheckDigit + ".";
alert(error);
return false;
}
} else { //length is 15
//check date
var date6 = idNumber.substring(6,12);
if (checkDate(date6) == false) {
alert("Date Info Incorrect.");
return false;
}
}
alert ("Correct.");
return true;
}function checkDate(dt) {
if (dt.length == 6) {
var d = new Date();
yy = d.getYear().toString().substring(2,4);
if (dt > yy) dt = "19" + dt;
else dt = "20" + dt;
}
yyyy = dt.substring(0,4);
mm = dt.substring(4,6);
dd = dt.substring(6,8);
if (mm > "12" || mm < "01") return false;
if (dd > "31" || dd < "01") return false;
else if (dd > "30") {
if (mm=="02" || mm=="04" || mm=="06" || mm=="09" || mm=="11") return false;
} else if (dd > "29") {
if (mm=="02") return false;
} else if (dd == "29") {
if (mm=="02" && (yyyy%4 != 0 || (yyyy%100 == 0 && yyyy%400 != 0))) return false;
}
return true;
}
var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
function cidInfo(sId){
var iSum=0
var info=""
if(!/^\d{17}(\d|x)$/i.test(sId))return false;
sId=sId.replace(/x$/i,"a");
if(aCity[parseInt(sId.substr(0,2))]==null)return "Error:非法地区";
sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
var d=new Date(sBirthday.replace(/-/g,"/"))
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "Error:非法生日";
for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
if(iSum%11!=1)return "Error:非法证号";
return aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女")
}document.write(cidInfo("380524198002300016"),"<br/>");
document.write(cidInfo("340524198002300019"),"<br/>")
document.write(cidInfo("340524197711111111"),"<br/>")
document.write(cidInfo("34052419800101001x"),"<br/>");
document.write(cidInfo("220283198011200331"),"<br/>");
</script>
<html>
<head>
<script language="javascript1.2">
<!-- start hiding
function verifyAddress(obj)
{
var email = obj.email.value;
var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
flag = pattern.test(email);
if(flag)
{
alert(“Your email address is correct!”);
return true;
}
else
{
alert(“Please try again!”);
return false;
}
}
// stop hiding -->
</script>
</head>
<body>
<form onSubmit="return verifyAddress(this);">
<input name="email" type="text">
<input type="submit">
</form>
</body>
</html>
<script>
var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
//检查身份证:15位或18位
function checkIdCard(sId){
var iSum=0
var info=""
var idCardLength = sId.length
if(!/^\d{17}(\d|x)$/i.test(sId)&&!/^\d{15}$/i.test(sId)) return false;
sId=sId.replace(/x$/i,"a");//在后面的运算中x相当于数字10,所以转换成a
if(aCity[parseInt(sId.substr(0,2))]==null)return false;
if (idCardLength==18) {
sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
var d=new Date(sBirthday.replace(/-/g,"/"))
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return false;
for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
if(iSum%11!=1)return false;
} else if (idCardLength==15) {
sBirthday=sId.substr(6,2)+"-"+Number(sId.substr(8,2))+"-"+Number(sId.substr(10,2));
var d=new Date(sBirthday.replace(/-/g,"/"))
if(sBirthday!=(d.getFullYear().toString().substr(2,2)+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return false;
}
return true;
}
</script>
var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
function cidInfo(sId){
var iSum=0
var info=""
if(!/^\d{17}(\d|x)$/i.test(sId))return false;
sId=sId.replace(/x$/i,"a");
if(aCity[parseInt(sId.substr(0,2))]==null)return "Error:非法地区";
sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
var d=new Date(sBirthday.replace(/-/g,"/"))
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "Error:非法生日";
for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
if(iSum%11!=1)return "Error:非法证号";
return aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女")
}document.write(cidInfo("380524198002300016"),"<br/>");
document.write(cidInfo("340524198002300019"),"<br/>")
document.write(cidInfo("340524197711111111"),"<br/>")
document.write(cidInfo("34052419800101001x"),"<br/>");
</script>判断电子邮件的
<script language="javascript1.2">
<!-- start hiding
function verifyAddress(obj)
{
var email = obj.email.value;
var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
flag = pattern.test(email);
if(flag)
{
alert(“Your email address is correct!”);
return true;
}
else
{
alert(“Please try again!”);
return false;
}
}
// stop hiding -->
</script>