表单验证,验证邮件、手机号,使其有效,求高手赐教!!!谢
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.<html xmlns="http://www.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>验证</title>
<style type="text/css">
.r{border:2px solid #f00;}
#tip{font-size:14px;color:red;}
</style>
</head>
<body>
<form action="#" method="post" name="myform" onsubmit="return chkform('myform')" >
<div id="tip"></div>
用 户 名:<input type="text" name="uname" onclick="this.className='';" /><br /><br />
密 码:<input type="password" name="pwd" onclick="this.className='';" /><br /><br />
邮 件:<input type="text" name="yj" onclick="this.className='';" /><br /><br />
手 机 号:<input type="text" name="sjh" onclick="this.className='';" /><br /><br />
身 份 证:<input type="text" name="sfzh" onclick="this.className='';" /><br /><br />
兴趣爱好:
<ul>
<li>1、听歌<input type="checkbox" name="selectList" value="1" onclick="limitselect(this)"/></li>
<li>2、上网<input type="checkbox" name="selectList" value="2" onclick="limitselect(this)" /></li>
<li>3、聊天<input type="checkbox" name="selectList" value="3" onclick="limitselect(this)" /></li>
<li>4、打球<input type="checkbox" name="selectList" value="4" onclick="limitselect(this)" /></li>
<li>5、音乐<input type="checkbox" name="selectList" value="5" onclick="limitselect(this)" /></li>
<li>6、音乐<input type="checkbox" name="selectList" value="6" onclick="limitselect(this)"/></li>
<li>7、聊天<input type="checkbox" name="selectList" value="7" onclick="limitselect(this)" /></li>
<li>8、打球<input type="checkbox" name="selectList" value="8" onclick="limitselect(this)"/></li>
<li>9、音乐<input type="checkbox" name="selectList" value="9" onclick="limitselect(this)" /></li>
<li>10、音乐<input type="checkbox" name="selectList" value="9" onclick="limitselect(this)" /></li>
</ul>
验 证 码:<input type="text" name="chkcode" onclick="this.className='';" />
<!--<input name="yzm" type="text" readonly="readonly" id="chkcode" onclick="showcode()"/>-->
<span onclick="showcode()" style="cursor:pointer;" id="chkcode" title="点击刷新"></span>
<br />
<input type="submit" value="提交">
</form>
<script type="text/javascript">
function chkform(sfz)
{
var formObj=document.forms[formname];
var tipObj=document.getElementById('tip');
with(formObj)
{
if(uname.value=='')
{
tipObj.innerHTML='用户名不能为空!';
uname.className='r';uname.focus();
return false;
}
else if(pwd.value=='')
{
tipObj.innerHTML='密码不能为空!';
pwd.className='r';pwd.focus();
return false;
}
else if(yj.value=='')
{
tipObj.innerHTML='邮箱不能为空!';
yj.className='r';yj.focus();
return false;
}
else if(sjh.value=='')
{
tipObj.innerHTML='手机号不能为空!';
sjh.className='r';sjh.focus();
return false;
}
else if(sfzh.value=='')
{
tipObj.innerHTML='邮箱不能为空!';
sfzh.className='r';sfzh.focus();
return false;
}
else if(chkcode.value.toLowerCase()!=getCurCookie('chkcode').toLowerCase())
{
tipObj.innerHTML='验证码错误!';
chkcode.className='r';chkcode.focus();
return false;
}
}
}
var maxselect=2,num=0;
function limitselect(Obj)
{
if(Obj.checked)
{
if(num>=maxselect)
{
Obj.checked=false;
alert('So Sorry!最多选'+maxselect+'个');
}
else
{
num++;
}
}
else
{
num--;
}
}
/*function chkform_yj(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@")
dotpos=value.lastIndexOf(".")
if (apos<1||dotpos-apos<2)
{alert(alerttxt);return false}
else {return true}
}
}
function chkform(myform)
{
with (myform)
{
if (chkform_yj(yj,"Not a valid e-mail address!")==false)
{yj.focus();return false}
}
}*/
/*身份证验证*/
function chksfz(sd)
{
var sid=sd;
if (sid.length != 18)
{
return false;
}
var ysid=sid;
sid=sid.substring(0,17);
var jiaquan = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
var jiaoyan = new Array('1','0','X','9','8','7','6','5','4','3','2');
var sigma=0;
for (var i = 0;i < 17;i++)
{
sigma +=parseInt(sid[i])*jiaquan[i];
}
if (sid+jiaoyan[(sigma % 11)]==ysid)
{
return true;
}
else
{
return false;
}
}
/*手机验证*/
function isMobile(sj){
if(/^13\d{9}$/g.test(value)||(/^15[0-35-9]\d{8}$/g.test(value))||(/^18[05-9]\d{8}$/g.test(value)))
{return true;}
else
{return false;}
}
/*var x='123456789199';
if (!isMobile(x))
{
alert('这个是哪个运营商的手机号啊?你们自家生产的吗?!');
}
else{
alert('OK.go away!');
}*/
/*邮件验证*/
function jsmail(yj){
return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(mail));
}
function isEmail(theStr){
var atIndex = theStr.indexOf('@');
var dotIndex = theStr.indexOf('.', atIndex);
// var flag = true;
theSub = theStr.substring(0,dotIndex+1);
if ((atIndex < 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex < atIndex + 2)||(theStr.length <= theSub.length))
/*
atIndex < 1表示@符号之前必须有一个或者多个字符
atIndex != theStr.lastIndexOf('@')表示在email当中只能有1个@
dotIndex < atIndex + 2表示@到.号之间必须有1个以上字符
theStr.length <= theSub.length表示.号至少两个以上字符
*/
{return false;}
else
{return true;}
}
function showcode()
{
var codestr='sadjkljaskd123456';
var output='';
for(var i=0;i<4;i++)
{
output+=codestr[ Math.floor( Math.random()*(codestr.length) ) ];
}
delCookie('chkcode');
setCookie('chkcode',output);
document.getElementById('chkcode').innerHTML=output;
}
//在页面执行第一次的时候,显示验证码
showcode();
/*
不管你以什么数据类型保存进了cookie,那么取出来之后,都会将是字符串!
在cookie中,任何数据,只能以字符串形式存在!
*/
function setCookie(Cookie_name,value)
{
var Then = new Date();
Then.setTime(Then.getTime() + 300000 ); //5分钟
document.cookie = Cookie_name+"="+value+"; expires="+ Then.toGMTString()
//Cookie_name=value; expries=time
}
function getCurCookie(Cookie_name)
//cur -> current
{
//var cookieString = new String(document.cookie);
var cookieString=document.cookie;
var cookiearr=cookieString.split('; ');
for(var c=0;c<cookiearr.length;c++)
{
var ccc=cookiearr[0].split('=');
if(Cookie_name==ccc[0])
{
return ccc[1];
}
else
{
return null;
}
}
}
function delCookie(Cookie_name)
{
document.cookie = Cookie_name+"=; expires=Fri, 02-Jan-1970 00:00:00 GMT";
}
</script>
</body>
</html>
[ 本帖最后由 shmily忆海 于 2012-3-19 08:23 编辑 ]