查看文章 |
javascript验证邮箱格式代码
2008年03月26日 星期三 下午 1:46
用一个正侧表达式在javascript中验证就是咯! ---------------------------------------------------------------------------------------------------------------------------- js:
function isEmail(email)
{ invalidChars = " /;,:{}[]|*%$#!()`<>?"; if (email == "") { return false; } for (i=0; i< invalidChars.length; i++) { badChar = invalidChars.charAt(i) if (email.indexOf(badChar,0) > -1) { return false; } } atPos = email.indexOf("@",1) if (atPos == -1) { return false; } if (email.indexOf("@", atPos+1) != -1) { return false; } periodPos = email.indexOf(".",atPos) if(periodPos == -1) { return false; // and at least one "." after the "@" } if ( atPos +2 > periodPos) { return false; // and at least one character between "@" and "." } if ( periodPos +3 > email.length) { return false; } return true; } aspx: 调用:
<input id=zz><input type=button value=check onclick="if(isEmail(zz.value))alert('正确');else alert('错误')"> |
最近读者:

