The example shows js's method of verifying that a form item is empty

  • 2020-11-25 07:09:35
  • OfStack

Forms authentication in almost every one need to register or log in website is indispensable, some of the validation is very complex, is various to your request, but this section only under 1 introduced form one of the most simple way of validation, is to judge whether is empty, some require lower site has already meet the need.
The code is as follows:


<html> 
<head> 
<meta charset="gb2312"> 
<title>js Simple Form validation </title> 
<script type="text/javascript">
window.onload=function()
{
 var bt=document.getElementById("bt");
 bt.onclick=function()
 {
  if(document.myform.name.value=="")
  {
   alert(" The user name cannot be empty !");
   document.myform.name.focus();
   return false;
  } 
  else if(document.myform.pw.value=="")
  {
   alert(" The password cannot be empty !");
   document.myform.pw.focus();
   return false; 
  }
 }
}
</script>
</head>
<body>
<form action="index.php" method="get" name="myform">
<ul>
 <li> The name :<input type="text" name="name" id="name" /></li>
 <li> password :<input type="text" name="pw" id="age" /></li>
 <li><input type="submit" id="bt"/></li>
</ul>   
</form>
</body>
</html>

Above is the detailed content of this article, hope to be helpful to your study.


Related articles: