Help needed for Form Validation please help!

Ads

 
 
 

Share on Google+Share on Google+

Akshay
Help needed for Form Validation please help!
2 Answer(s)      5 years and 4 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>
Ads
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:

<[email protected] 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


Ads









Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training code for programming How can I dynamically load user-entered data as content of jquery dialog? java webcam integration question java coding Fix Scrollbar position in a checkboxList after postback Information Save Alert Country wise redirection to files Interface in javascript connect database with javascript Zip Code ZipCode how to validate the email login How to play FLV video files on Ipad using HTML code or javascript code? Nitobi Grid is not working in IE10/IE11 jquery issue plese help RegEx Tree structure from json whose parents can be dragged and dropped from child newyorklife insurance getElementById not working javascript code for search in html file Implement The Skeleton Code Implement This Skeleton Code Mobile Automation using Appium Computer forensic application Java: Searching a string from doc files java linked list urgent !!! java linked list urgent!!! library management library management library management library management question Html form using JavaScript to display the table content java how to prevent no from unroundin off on clicking text box again in JSP and Jquery graph generation from xml design file display data from database in textbox when id entered in textbox Javascript Code for all fields how to get HTTP GET request value using javascript array Javascript Querry autocomplete(): Spring mvc with jquery: I am not getting correct value in the text filed. Please help me Create SpreadSheet same as excel using java applet Create SpreadSheet same as excel using java applet How to change focus among multiple select boxes? Write JQUERY Code Write JQUERY Code Converting jsp variable to java variable java

Ads

 
Advertisement null

Ads