• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to check if textboxes are null

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I prefer to check not only empty, but blank:

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jitesh,

U must use javascript for validation becoz it work on client side so u r request processing will fast.
Thanx
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Bear,

For suggesting me.
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic