2016-05-21 255 views
0

我是jsp編程新手。我開發了註冊表單,其中我無法驗證密碼和確認密碼字段。當密碼和確認密碼不匹配時,它會轉到下一頁但它錯了。它檢查兩個字段是相等然後它進入下一頁。並且在註冊頁面數據被插入到數據庫中。 請考慮下面的代碼。如何使用jsp驗證密碼並確認密碼字段?

<script> 
    function myFunction() { 
     var pass1 = document.getElementById("pass").value; 
     var pass2 = document.getElementById("rpass").value; 
     if (pass1 != pass2) { 
      //alert("Passwords Do not match"); 
      document.getElementById("pass").style.borderColor = "#E34234"; 
      document.getElementById("rpass").style.borderColor = "#E34234"; 
     } 
     else { 
      alert("Passwords Match!!!"); 
      document.getElementById("regForm").submit(); 
     } 
    } 
</script> 
<form method="post" id="regForm" onsubmit="return myFunction()" action="registration.jsp"> 

    First Name 
    <input type="text" name="fname" value="" /> 

    Email 
    <input type="text" name="email" value="" /> 

    Password</td> 
    <input type="password" name="pass" value="" /> 

    Re Password 
    <input type="password" name="rpass" value="" /> 

    <input type="submit" value="Submit" />          
</form> 

registration.jsp插入DB代碼

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ page import ="java.sql.*" %> 
<%@page import="java.sql.DriverManager"%> 

<% 
    String user = request.getParameter("uname");  
    String pwd = request.getParameter("pass"); 
    String fname = request.getParameter("fname"); 
    String lname = request.getParameter("lname"); 
    String email = request.getParameter("email"); 

    Class.forName("com.mysql.jdbc.Driver"); 
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/app", 
      "root", ""); 
    Statement st = con.createStatement(); 
    int i = st.executeUpdate("insert into members(first_name, last_name, email, uname, pass,regdate) values ('" + fname + "','" + lname + "','" + email + "','" + user + "','" + pwd + "',CURDATE())"); 
    if (i > 0) { 
     out.print("Registration Successfull!"+"<a href='welcome.jsp'>Go to Login</a>"); 
    } else { 
     response.sendRedirect("index.jsp"); 
    } 
%> 

回答

1

getElementById()方法將返回具有與指定的值的ID屬性的元素。 沒有定義標識你是怎麼進入document值 這樣定義ID以及所有文本框和其他領域一樣

First Name 
<input type="text" name="fname" id="fname" value="" /> 

Email 
<input type="text" name="email" id="email" value="" /> 

Password</td> 
<input type="password" name="pass" id="pass" value="" /> 

Re Password 
<input type="password" name="rpass" id="rpass" value="" />