JavaScript's method for determining that a form is empty and getting focus

  • 2020-12-13 18:51:14
  • OfStack

This article illustrates how JavaScript can determine if a form is empty and get focus. To share 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 Get focus </title>
</head>
<script language="JavaScript">
  function aa()
  {
   if(document.forms[0].elements[0].value.length==0)
   { alert(" Do not leave your name empty ");
   document.forms[0].elements[0].focus();
   return false;
  }
  else if(document.forms[0].elements[1].value.length==0)
   { alert(" Password must not be empty ");
   document.forms[0].elements[1].focus();
   return false;
  }else {return true;}
  }
</script>
<body>
<form action="/ok.jsp" method="post" name="form1" onSubmit="return aa();">
 The user name <input type="text" name="name" >
 password <input type="password" name="password">
<input type="submit" value=" submit ">
</form>
</body>
</html>

I hope this article has been helpful in JavaScript programming.


Related articles: