表单验证制作方法!


  <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 全部选择 提示:你可先修改部分代码,再按运行]

文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 441
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 20 字 | UBB代码 关闭 | [img]标签 关闭