日期:2014-05-16  浏览次数:20303 次

javascript中表单校验中遇到的校问题




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
table{
border:#0066ff 1px solid;
width:600px;
border-collapse:collapse;
}
table th,table td{
border:#0066ff 1px solid;
padding:10px;
}
table td{
background-color:#ffff99;
}
table th{
background-color:#ff9900;
}
#repswspan{
margin-left:115px;
}
.errorinfo{
color:#F00;
display:none;
}
.focus{
border:#09f 2px solid;
}
.norm{
border:#999999 1px solid;
}
.error{
border:#f00 2px solid;
}
</style>

<script type="text/javascript">
function inputColor(input){
input.className="norm";
input.onfocus=function(){
this.className="focus";
}
}

window.onload=function(){
with(document.forms[0]){
inputColor(user);
inputColor(psw);
inputColor(repsw);
inputColor(mail);
}
}

function check(inputNode,regex,divId){
var b=false;
var divNode=document.getElementById(divId);

if(regex.test(inputNode.value)){
inputNode.className="norm";
divNode.style.display="none";
b=true;
}else{
inputNode.className="error";
divNode.style.display="block";
}
return b;
}

function checkUser(userNode){
var regex=/^\w{3,5}$/;
return check(userNode,regex,"userdiv");
}


function checkPsw(pswNode){
var regex=/^[a-z0-9]{3,5}$/i;
return check(pswNode,regex,"pswdiv");
}

function checkRepsw(repswNode){
var b=false; 
var value1=repswNode.value;
var value2=document.getElementsByName("psw")[0].value;
var divNode=document.getElementById("repswdiv");
if(value1==value2){
repswNode.className="norm";
divNode.style.display="none";
b=true;
}else{
repswNode.className="error";
divNode.style.display="block";
}
return b;
}

function checkMail(mailNode){
var regex=/^\w+@\w+(\.\w+)+$/;
return check(mailNode,regex,"maildiv");
}

function checkForm(formNode){
with(formNode){
if(checkUser(user)&&checkPsw(psw)&&checkRepsw(repsw)&&checkMail(mail)){
event.returnValue=true;
}else{
event.returnValue=false;
}

}
</script>
</head>

<body>
<form onsubmit="checkForm(this)">
    <table>
        <tr>
            <th>注册表单</th>
        </tr>
       
        <tr>
            <td>
                <div>用户名</div>