wood burning stoves*
The moose likes JSP and the fly likes How to check if textboxes are null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to check if textboxes are null" Watch "How to check if textboxes are null" New topic
Author

How to check if textboxes are null

Jitesh Ranavaya
Greenhorn

Joined: Sep 09, 2004
Posts: 9
Hi, I am writing a 'form' where I need to check if text boxes are null (empty), before the next page is displayed. Here is my code so far:


<%@page import="reg.Register"%>
<jsp:useBean id = "newcust" class="reg.Register" scope ="session"/>

<jsp:setProperty name ="newcust" property="*" />



<% if(request.getParameter("Continue")!=null){

%>

<%!String lname="";%>



<%
if (request.getParameter("lastName")==null){

lname = "Please enter last name";
response.sendRedirect("Register1.jsp");


}
%>


<jsp:forward page="Register2.jsp"/>
<%}%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
</head>

<body>

<span id="Layer1" style="position:absolute; left:610px; top:15px; width:358px; height:23px; z-index:10">
<div align="right"><font color="#0000FF" size="3" face="Lucida Console"><a href="FrontPage.jsp">Home</a>
<a href="SignIn.jsp">Sign In</a> <a href="SignOut.jsp">Sign Out</a> <font color="#000000">Register</font>
Help</font></div>
</span>
<div id="Layer2" style="position:absolute; left:9px; top:82px; width:137px; height:83px; z-index:11">
<table width="146" border="0" cellpadding="2">
<tr>
<td><font color="#0000FF" size="3" face="Lucida Console">My Bazaar</font></td>
</tr>
<tr>
<td><font color="#0000FF" size="3" face="Lucida Console">Browse</font></td>
</tr>
<tr>
<td height="20"><font color="#0000FF" size="3" face="Lucida Console"><a href="Sell.jsp">Sell</a></font></td>
</tr>
<tr>
<td><font color="#0000FF" size="3" face="Lucida Console">Search</font></td>
</tr>
</table>
</div>

<div id="Layer3" style="position:absolute; left:785px; top:509px; width:96px; height:17px; z-index:15"><font color="#FF0000" face="Lucida Console">*</font><font color="#FF0000">required
fields</font></div>
<form method = post action = "Register1.jsp">
<div id="Layer5" style="position:absolute; left:191px; top:126px; width:351px; height:255px; z-index:14"> <form name="form1" method="post" action="">
<table width="342" height="389" border="0" cellpadding="2">
<tr>
<td width="63"><font color="#FF0000" face="Lucida Console">*</font><font face="Lucida Console">Title
: </font></td>
<td width="145"><font face="Lucida Console">
<select name="title">
<option>Mr</option>
<option>Mrs</option>
<option>Ms</option>
</select>
</font></td>
</tr>
<tr>
<td><font color="#FF0000" face="Lucida Console">*</font><font face="Lucida Console">Surname
: </font></td>
<td><font face="Lucida Console">


<%=lname%>


<input name="lastName" type="text" id="lastName">
</font></td>
</tr>
<tr>
<td><font color="#FF0000" face="Lucida Console">*</font><font face="Lucida Console">Firstname
: </font></td>
<td><font face="Lucida Console">
<input name="firstName" type="text" id="firstName">
</font></td>
</tr>
<tr>
<td height="52"><font color="#FF0000" face="Lucida Console">*</font><font face="Lucida Console">Street
Address : </font></td>
<td><font face="Lucida Console">
<input name="addressLine1" type="text" id="addressLine1">
</font></td>
</tr>
<tr>
<td><font face="Lucida Console"> </font></td>
<td><font face="Lucida Console">
<input name="addressLine2" type="text" id="addressLine1">
</font></td>
</tr>
<tr>
<td><font color="#FF0000" face="Lucida Console">*</font><font face="Lucida Console">City
: </font></td>
<td><font face="Lucida Console">
<input name="city" type="text" id="city">
</font></td>
</tr>
<tr>
<td><font color="#FF0000" face="Lucida Console">*</font><font face="Lucida Console">County
: </font></td>
<td><font face="Lucida Console">
<input name="county" type="text" id="county">
</font></td>
</tr>
<tr>
<td><font color="#FF0000" face="Lucida Console">*</font><font face="Lucida Console">Postcode
: </font></td>
<td><font face="Lucida Console">
<input name="postCode" type="text" id="postCode">
</font></td>
</tr>
<tr>
<td><font color="#FF0000" face="Lucida Console">*</font><font face="Lucida Console">Phone
number :</font></td>
<td><font face="Lucida Console">
<input name="phone" type="text" id="phone">
</font></td>
</tr>
<tr>
<td><font color="#FF0000" face="Lucida Console">*</font><font face="Lucida Console">Country
: </font></td>
<td><font face="Lucida Console">
<input name="Country" type="text" id="Country">
</font></td>
</tr>
<tr>
<td><font color="#FF0000" face="Lucida Console">*</font><font face="Lucida Console">Date
of Birth : </font></td>
<td><font face="Lucida Console">
<input name="dob" type="text" id="dob">
</font></td>
</tr>
<tr>
<td><font face="Lucida Console"> </font></td>
<td><font face="Lucida Console">
<input name="Continue" type="submit" id="Continue" value="Continue">
</font></td>
</tr>
</table>
</form>
</div>
<div align="right"></div>
<span id="Layer9" style="position:absolute; left:613px; top:33px; width:172px; height:38px; z-index:9"><img src="l.jpg" width="389" height="48"></span>
<div align="right">
<p> </p>
<p align="left">  </p>
</div>
</body>
</html>

The above does not work (and you can probably tell that am new to jsp).
lname is the attribute that I want to display the error message, obviously above the lastName text box. The problem I am having is that I dont know how to stucture the if (if-else) statements.

Thanks for the help in advance
Jitesh
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Hope you can use a JavaScript, that validates your form on submit and return false if there is any error, else return true..
[ November 08, 2004: Message edited by: srini vasan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
The content of type="text" inputs is never null, you will get an empty string "" if the user does not fill it in.
Bill
Jitesh Ranavaya
Greenhorn

Joined: Sep 09, 2004
Posts: 9
William, thats what I meant, an empty string.
How do I check that all of the text boxes are filled in and not left blank?
There must be an efficient way of doing it.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

Actually, I prefer to check not only empty, but blank:



Customer surveys are for companies who didn't pay proper attention to begin with.
pradeep jaladi
Ranch Hand

Joined: Nov 21, 2004
Posts: 65
After submitting the Values forward to the validation.jsp in that validate the parameters if any parameter is null or Blank simply .redirect it back to ur First page or forward to the next page.


Pradeep.Jaladi
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
Hi,

You check for empties in a javascript function eg you have a form named
frm1 with a text field named myname and a submit button.

Point:
You may either use
1) length of textfield value or
2) text of textfield
to check out the textfield.

Here is an example:

Hope this solves your problem.

Maki Jav
[ December 18, 2004: Message edited by: Maki Jav ]

Help gets you when you need it!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Using JavaScript should not be your option since it can be disabled. Yes you can use it, but you should always have the checks with your server side code.

Eric
Nitin Jawarkar
Ranch Hand

Joined: Dec 18, 2004
Posts: 79
hi jitesh,

U must use javascript for validation becoz it work on client side so u r request processing will fast.
Thanx


Cheers<br />Nitin
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Nitin,

JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.

I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.

thanks,
bear
JSP Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

U must use javascript for validation becoz it work on client side so u r request processing will fast.


If, by this, you mean that by performing validation on the client you can forgoe server-side validation (to make server-side processing faster), then this is very poor advice indeed.

Always, always, ALWAYS, and did I mention always perform server-side validation regardless of whether you perform client-side validation. Not only can Javascript be turned off, script kiddies just love to probe your site by spoofing requests looking for vulnerabilities.
Nitin Jawarkar
Ranch Hand

Joined: Dec 18, 2004
Posts: 79
Thanx Bear,

For suggesting me.
 
Consider Paul's rocket mass heater.
 
subject: How to check if textboxes are null
 
Similar Threads
build a data entry sheet like spread-sheet
JSP Help Regarding the Radio Button
document.forms[0].submit(); throws Invalid Argument in IE
problem in getting the request parameters
Run Time error