JavaScript表单不为空验证

快乐打工仔 分类:实例代码

JavaScript表单不为空验证属于前端实例代码,有关更多实例代码大家可以查看

表单不为空验证是最为简单的表单验证,只会用在较为简易的表单验证。

下面就分享一段类似简单代码实例,希望能够对初学者有所借鉴作用。

代码实例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<title>前端教程网</title>
<script type="text/javascript">        
function formcheck(){
  if(document.form.zhanghao.value==""){
    alert("号码不能为空!");
    this.form.zhanghao.focus();
    return false;
  }
  else if(document.form.content.value==""){
    alert("内容不能为空!")
    this.form.content.focus();
    return false;
  }
  else {
    form.submit(); 
  } 
}
window.onload=function(){
  var bt=document.getElementById("bt");
  bt.onclick=function(){formcheck();}
}
</script>
</head>
<body>
<form name="form" method="post">
  <table>
    <tr>
      <th colspan="8" class="CTitle">发送短信</th>
    </tr>
    <tr bgcolor="#eeeeee">
      <td align="center">号   码:</td>
      <td><textarea name="content" cols="80" rows="15" id="zhanghao" class="formh"></textarea></td>
    </tr>
    <tr bgcolor="#eeeeee">
      <td align="center">内   容:</td>
      <td><textarea name="content" cols="80" rows="15" id="content" class="formh"></textarea></td>
    </tr>
    <tr bgcolor="#eeeeee" align="center">
      <td height="25" colspan="2">
        <input type="button" name="Submit" value="发 送" id="bt"/>
        <input type="reset" name="Submit2" value="重 置" class="button"/>
      </td>
    </tr>
  </table>
</form>
</body>
</html>

JavaScript表单不为空验证,这样的场景在实际项目中还是用的比较多的,关于JavaScript表单不为空验证就介绍到这了。

回复

我来回复
  • 暂无回复内容