1. 程式人生 > >表單提交的客戶端驗證(通過js函式驗證)

表單提交的客戶端驗證(通過js函式驗證)

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'JiaoYan.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 <script type="text/javascript">
  function check(fom){
   if (fom.user.value=="") {
    alert("使用者名稱不能為空!");
    fom.user.focus();
    return false;
   }
   if (fom.psw.value=="") {
    alert("密碼不能能夠為空!");
    fom.psw.focus();
    return false;
   }
   if (fom.psw2.value!=fom.psw.value) {
    alert("兩次輸入的密碼不一致!請重新輸入!");
    fom.psw.focus();
    return false;
   }
   if (fom.email.value=="") {
    alert("郵箱不能為空!");
    fom.email.focus();
    return false;
   }
   if (fom.email2.value!=fom.email.value) {
    alert("兩次輸入的郵箱地址不一致!請重新輸入!");
    fom.email.focus();
    return false;
   }
  }
 </script>
  </head>
 
  <body>
   <form action="" name="fom" method="post" onsubmit="return check(this)">
    <p align="center">使用者註冊</p>
    <p align="center">使用者名稱:<input type="text" name="user">
    </p>
    <p align="center">密碼:<input type="password" name="psw">
    </p>
    <p align="center">確認密碼:<input type="password" name="psw2">
    </p>
    <p align="center">輸入郵箱:<input type="text" name="email">
    </p>
    <p align="center">確認郵箱:<input type="text" name="email2">
    </p>
    <p align="center"><input type="submit" value="提交">
     <input type="reset" value="重置">
    </p>
   </form>
  </body>
</html>