Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 15:20:49
Original
1276 people have browsed it

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>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!