Home  >  Article  >  Web Front-end  >  Examples to explain js method to verify whether form items are empty_javascript skills

Examples to explain js method to verify whether form items are empty_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:20:491277browse

Form verification is essential for almost every website that requires registration or login. Some verifications are very complicated and can be said to be various requirements for you. However, this chapter only introduces the simplest form. The verification method is to determine whether it is empty. Some websites with lower requirements have already met this need.
The code is as follows:

<html> 
<head> 
<meta charset="gb2312"> 
<title>js简单表单验证</title> 
<script type="text/javascript">
window.onload=function()
{
 var bt=document.getElementById("bt");
 bt.onclick=function()
 {
  if(document.myform.name.value=="")
  {
   alert("用户名不能为空!");
   document.myform.name.focus();
   return false;
  } 
  else if(document.myform.pw.value=="")
  {
   alert("密码不能为空!");
   document.myform.pw.focus();
   return false; 
  }
 }
}
</script>
</head>
<body>
<form action="index.php" method="get" name="myform">
<ul>
 <li>姓名:<input type="text" name="name" id="name" /></li>
 <li>密码:<input type="text" name="pw" id="age" /></li>
 <li><input type="submit" id="bt"/></li>
</ul>   
</form>
</body>
</html>

The above is the detailed content of this article, I hope it will be helpful to everyone’s study.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn