Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JavaScriptQuestions Help needed for Form Validation please help!
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Akshay
Help needed for Form Validation please help!
2 Answer(s)      2 years and 8 months ago
Posted in : JavaScript Questions


I have created a page which has login as well as Signup fields. I validated the login part by using jsp as it has to go through the database for checking for incorrect Username/password combination . Problem arises while validating the Sign up part as after clicking the submit button for sign up it also goes through the jsp code and returns incorrect Username/password combination. So how can I make the jsp code confined to the login submit button.

<html><form name=f1 method=POST action=log.jsp>

<body background="img.jpg">
<div align="right"><b><font size=4 color=#651A0A>
Username  <input type=text name=username> 
Password  
<input type=password name=password> 
</font>  
<script type="text/javascript">
function validateForm()
{
var x=document.forms["f2"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }

}</script>
<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%@ page language="java" session="true" %>

<%
        //Getting the text values from create login page
        String uname   = request.getParameter("username");
        String pass  = request.getParameter("password");



        java.sql.Connection con;
        java.sql.Statement s;
        java.sql.ResultSet rs , rs1;


        con=null;
        s=null;
        rs=null;
        rs1=null;

        String u  =  null;
        String p =  null;

        int i=0;


        try{

               Class.forName("org.gjt.mm.mysql.Driver");
   con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/test","akshay","");
              // out.println("1");
           }
        catch(ClassNotFoundException cnfex)
        {
               cnfex.printStackTrace();
        }

        String sql = "select * from us where uname='"+uname+"'";
        try{
                s = con.createStatement();
                rs = s.executeQuery(sql);
        while( rs.next() )
        {
             u = rs.getString("uname");
             p  = rs.getString("pwd");
             //i++;
        }


        if(uname.equals(u) && pass.equals(p))
        {
        response.sendRedirect("http://localhost:8080/main.jsp");


        }
        else
        { %><br><%
           out.println("incorrect username/password combination");
        }
        }
        catch(Exception e){e.printStackTrace();}
        finally{
        if(rs!=null) rs.close();
        if(s!=null) s.close();
        if(con!=null) con.close();
        }

%>

<input type=Submit name=login value="LOGIN" onclick="this.form.target='_blank';return true;">
  </div>
<form name=f2 method=POST action="Serv" onsubmit="return validateForm()" ><table noborder cellpading= 8 width= 100%>
<tr><th align="left"><iframe width = 65% height= 80% name=iframe src="fade1.html" frameborder=0 scrolling=no ></iframe><div align="center">
<a href="main.html" ><button accesskey=a>
<table style="color: white; " align="center" class="border" bgcolor=blue width="100%" background="back/blue_type.png" onmouseover="this.background='back/reset4.jpg'" onmouseout="this.background='back/blue_type.png'">
<tr>
<td width="81%"><font color=white size=1><b>ADVERTISE HERE</b></font></td>
<td width="19%"><img src="back/reset4.jpg" onmouseover="this.src='back/blue_type.png'" onmouseout="this.background='back/reset4.jpg'" /></td>
</tr>
</table>
</button></a>         
            </div> </th>
<TD align="right"><br><font size=5 color=white><b><u><rt>SIGN UP</u>              </b></font><br><br><font color=white>
First Name <input type=text name=fname><br>
Last Name <input type=text name=lname><br>
Username <input type=text name=uname><br>
Password <input type=password name=pwd><br><br>
<div align="right">      
><input type=submit value="SIGNUP" onclick="f2.action='Serv';return true;">   
             </div><br> </td></form>
</html>

Advertisement
View Answers

January 3, 2012 at 10:46 AM


1)login.jsp:

<html>
<script>
function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
if(username==""){
 alert("Enter Username!");
  return false;
}
if(password==""){
 alert("Enter Password!");
  return false;
}
return true;
}
</script>
<form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();">
<table>
<tr><td>Username:</td><td><input type="text" name="user"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>
<%String msg=request.getParameter("msg");
if(msg!=null){
    %>
<label><font color="red"><%=msg%></font></label> 
<%
}
    %>

2)check.jsp:

<%@page import="java.sql.*"%>

<%
String user=request.getParameter("user");
String pass=request.getParameter("pass");
 Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'");
int count=0;
          while(rs.next())
          {

                   count++;
          }

                    if(count>0)
          {
            out.println("welcome "+user);
          }
          else
          {
                       response.sendRedirect("login.jsp?msg=Invalid Username or Password");
          }
%>


January 3, 2012 at 3:07 PM


Got it Thanks



Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com