Home > Web Front-end > JS Tutorial > JavaScript method to determine whether the form is empty and get focus_javascript skills

JavaScript method to determine whether the form is empty and get focus_javascript skills

WBOY
Release: 2016-05-16 15:15:52
Original
1264 people have browsed it

The example in this article describes how to use JavaScript to determine whether a form is empty and get focus. Share it with everyone for your reference, the details are as follows:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>javascript获得焦点</title>
</head>
<script language="JavaScript">
  function aa()
  {
   if(document.forms[0].elements[0].value.length==0)
   { alert("姓名不可以为空");
   document.forms[0].elements[0].focus();
   return false;
  }
  else if(document.forms[0].elements[1].value.length==0)
   { alert("密码不可以为空");
   document.forms[0].elements[1].focus();
   return false;
  }else {return true;}
  }
</script>
<body>
<form action="/ok.jsp" method="post" name="form1" onSubmit="return aa();">
用户名<input type="text" name="name" >
密码<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>

Copy after login

I hope this article will be helpful to everyone in JavaScript programming.

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