本帖最后由 汪小照 于 2012-9-22 12:16 编辑
<script type="text/javascript">
function check(name,reg,spaninfo,okinfo,errorinfo)
{
var flag;
var oNode = document.getElementsByName(name)[0];
var val = oNode.value;
var oSpanNode = document.getElementById(spaninfo);
if(reg.test(val))
{
oSpanNode.innerHTML = okinfo.fontcolor("green");
flag = true;
}
else
{
oSpanNode.innerHTML = errorinfo.fontcolor("red");
flag = false;
}
return flag;
}
//校验用户名
function checkUser()
{
var reg =/^\d{4}$/;
return check("user",reg,"user","用户名正确","用户名错误");
}
//校验密码
function checkPsw()
{
var reg=/^[a-z]{4}$/;
return check("psw",reg,"psw","密码正确","密码错误");
}
//确认密码
function checkRepsw()
{
var flag;
//获取密码框
var val_psw = document.getElementsByName("psw")[0].value;
//获取确认密码框
var val_repsw = document.getElementsByName("repsw")[0].value;
var ospan = document.getElementById("repsw");
if(val_repsw == val_psw)
{
ospan.innerHTML = "两次密码格式一致".fontcolor("green");
flag=true;
}
else
{
ospan.innerHTML = "两次密码格式不一致".fontcolor("red");
flag = false;
}
return flag;
}
//校验邮箱
function checkMail()
{
var reg =/^\w+@\w+(\.\w+)+$/;
return check("mail",reg,"mail","邮箱格式正确","邮箱格式错误");
}
function submitfrom()
{
var ofrom = document.getElementById("submit");
if(checkUser()&&checkPsw()&&checkRepsw()&&checkMail())
ofrom.submit();
}
function checkFrom()
{
if(checkUser()&&checkPsw()&&checkRepsw()&&checkMail())
return true;
else
return false;
}
</script>
<body>
<form id="submit"> //调用表单校验,返回一个值后,为甚么表单出错后还是能照样提交出去。
用户名称:<input type="text" name="user" /><span id="user"></span></br>
输入密码:<input type="password" name="psw"/><span id="psw" ></span></br>
确认密码:<input type="password" name="repsw"/><span id="repsw" ></span></br>
邮箱地址:<input type="text" name="mail"/><span id="mail"></span></br>
<input type="submit" value="提交数据"/>
</form>
<hr/>
<input type="button" value="提交数据"/>
以上标红色字迹 ,如何解决表单出错时不能提交出去的问题?
并说一下解决表单出错时不能提交出去的原理?(主要是我想知道这个原理)
|