Help needed for Form Validation please help!



Share on Google+Share on Google+

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> 
<input type=password name=password> 
<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;

<%@ page import="java.sql.*" %> 
<%@ page import="*" %> 
<%@ 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;


        String u  =  null;
        String p =  null;

        int i=0;


   con = DriverManager.getConnection("jdbc:mysql://","akshay","");
              // out.println("1");
        catch(ClassNotFoundException cnfex)

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

        if(uname.equals(u) && pass.equals(p))

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


<input type=Submit name=login value="LOGIN" onclick="'_blank';return true;">
<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'">
<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>
            </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>
View Answers

January 3, 2012 at 10:46 AM


function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
 alert("Enter Username!");
  return false;
 alert("Enter Password!");
  return false;
return true;
<form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();">
<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>
<%String msg=request.getParameter("msg");
<label><font color="red"><%=msg%></font></label> 


<[email protected] import="java.sql.*"%>

String user=request.getParameter("user");
String pass=request.getParameter("pass");
    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;


            out.println("welcome "+user);
                       response.sendRedirect("login.jsp?msg=Invalid Username or Password");

January 3, 2012 at 3:07 PM

Got it Thanks


Related Tutorials/Questions & Answers:
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


Advertisement null