<html >
</head>
<body>
<!--方式一:利用form表单的onsubmit事件提交内容,返回ture就自动提交,false不提交-->
<form name="form1" target="_blank" method="post" onsubmit="dosubmit1(this)">
会员号码(6位数字):<input type="text" name="mem_id" />
<input type="submit" name="submit1" value="递交" />
</form>
<script language="javascript" type="text/javascript">
function dosubmit1(frm) {
if (frm.mem_id.value.length != 6) {
alert("会员号必须是6位");
return false;
} else {
var mem_value = frm.mem_id.value;
for (var i = 0; i < mem_value.length; i++) {
if (mem_value.charAt(i) < '0' || mem_value.charAt(i) > '9') {
alert("会员号只能是数字");
return false;
}
}
}
//frm.submit(); 会返回两个
return true;
}
</script>
<!--方式二:利用form表单的onsubmit事件提交内容,不利用返回true提交,而用submit()提交-->
<form name="form2" target="_blank" method="post" onsubmit="dosubmit2(this);return false;">
会员号码(6位数字):<input type="text" name="mem_id" />
<input type="submit" name="submit1" value="递交" />
</form>
<script language="javascript" type="text/javascript">
function dosubmit2(frm) {
if (frm.mem_id.value.length != 6) {
alert("会员号必须是6位");
} else {
var mem_value = frm.mem_id.value;
for (var i = 0; i < mem_value.length; i++) {
if (mem_value.charAt(i) < '0' || mem_value.charAt(i) > '9') {
alert("会员号只能是数字");
}
}
}
frm.submit();
}
</script>
<!--方式三:不利用form表单的onsubmit事件,而利用按钮自身的
<form name="form3" target="_blank" method="post" >
会员号码(6位数字):<input type="text" name="mem_id" />
<input type="submit" name="submit1" onclick="dosubmit3(this.form);return false;" value="递交" />
</form> <!--this.form上层的表单对象-->
<script language="javascript" type="text/javascript">
function dosubmit3(frm) {
if (frm.mem_id.value.length != 6) {
alert("会员号必须是6位");
} else {
var mem_value = frm.mem_id.value;
for (var i = 0; i < mem_value.length; i++) {
if (mem_value.charAt(i) < '0' || mem_value.charAt(i) > '9') {
alert("会员号只能是数字");
}
}
}
frm.submit();
}
</script>
</body>
</html>