表单验证制作方法!
作者:cmscn 日期:2008-08-11
<form name="theform" action="aaa" method="POST" enctype=text/plain" onSubmit="return formCheck()">
<script language="javascript">
function formCheck() { if ("" == document.theform.name.value) {
alert("请输入你的姓名")
document.theform.name.focus()
return false
} if ("" == document.theform.address.value) {
alert("请输入你的联系地址")
document.theform.address.focus()
return false
}
if ("" == document.theform.zip.value) {
alert("请输入你的邮政编码")
document.theform.zip.focus()
return false
} if ("" == document.theform.tel.value) {
alert("请输入你的电话")
document.theform.tel.focus()
return false
} if ("" == document.theform.fax.value) {
alert("请输入你的传真")
document.theform.fax.focus()
return false
} if ("" == document.theform.email.value) {
alert("请输入邮件地址")
document.theform.email.focus()
return false
}
var email = document.theform.email.value
var is_error = false
var pn_0 = email.indexOf("@")
var pn_1 = email.indexOf(".",pn_0)
var pn_2 = email.length
if (pn_0<1 || pn_1<pn_0+2 || pn_1+2>pn_2) is_error=true
if (is_error) {
alert("请输入正确的邮件地址")
document.theform.email.focus()
return false
} if ("" == document.theform.remark.value) {
alert("请输入您的要求")
document.theform.remark.focus()
return false
}
return true
}
</script> <table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="25%">姓名:</td>
<td>
<input name=name style="FONT-SIZE: 12px; WIDTH: 250px">
*</td>
</tr>
<tr>
<td>联系地址:</td>
<td>
<input name=address style="FONT-SIZE: 12px; WIDTH: 250px">
*</td>
</tr>
<tr>
<td>邮编:</td>
<td>
<input name=zip style="FONT-SIZE: 12px; WIDTH: 250px">
* </td>
</tr>
<tr>
<td>联系电话:</td>
<td>
<input name=tel style="FONT-SIZE: 12px; WIDTH: 250px">
*</td>
</tr>
<tr>
<td>传真:</td>
<td>
<input name=fax style="FONT-SIZE: 12px; WIDTH: 250px">
*</td>
</tr>
<tr>
<td>E-mail :</td>
<td>
<input name=email style="FONT-SIZE: 12px; WIDTH: 250px">
*</td>
</tr>
<tr>
<td>您的要求 :</td>
<td>
<textarea name=remark rows=8 style="FONT-SIZE: 12px; WIDTH: 250px" ></textarea>
*</td>
</tr>
<tr>
<td> </td>
<td>
<input name=SUBMIT type=submit value=发送>
<input name=reset type=reset value=重置>
</td>
</tr>
</table>
</form>
var itemName=new Array("name","address","zip","tel","fax","email");
var itemAlert=new Array("姓名","地址","邮编","电话","传真","电邮");
for (i=0;i<document.body.all.tags("INPUT").length;i++) {
if ("" == document.theform.itemName[i].value) {
alert("请输入你的"+ itemAlert[i]);
document.theform.itemName[i].focus();
return false;
}
}
HTML代码
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
评论: 0 | 引用: 0 | 查看次数: 441
发表评论