Form processing questions
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Misty_Garrick
Posted On:   Tuesday, June 19, 2001 07:12 AM

Hi, I'm trying to process a form with five text fields. If the user leaves a field blank, it should not be processed. I can't figure out which comparison to use to see if the field is blank. I've tried null, "", and " ", but none of these pick up on the fact that the field is null and thus the blank field is still processed, which results in an internal servlet error with java.lang.NumberFormatException as its root cause. Anyone know how to get around this? BTW, the form works fine if all fields are filled. Oh, and one more thing...I'm trying to code this as a loop and I want to create new parameter names "value_1", "value_2", etc. each time through. Right now, I've just kind   More>>

Hi,



I'm trying to process a form with five text fields. If the user leaves a field blank, it should not be processed. I can't figure out which comparison to use to see if the field is blank. I've tried null, "", and " ", but none of these pick up on the fact that the field is null and thus the blank field is still processed, which results in an internal servlet error with java.lang.NumberFormatException as its root cause. Anyone know how to get around this? BTW, the form works fine if all fields are filled.



Oh, and one more thing...I'm trying to code this as a loop and I want to create new parameter names "value_1", "value_2", etc. each time through. Right now, I've just kind of hacked it with the NumStrings array below. Is there a way to cast an int to a string, or something like that?



Thanks,
Misty

mgarrick@crcmn.com



			
<% String[] Amount;
Amount = new String[5];

Amount[0] = request.getParameter("Amount1");
Amount[1] = request.getParameter("Amount2");
Amount[2] = request.getParameter("Amount3");
Amount[3] = request.getParameter("Amount4");
Amount[4] = request.getParameter("Amount5");

String[] NumStrings = {"1", "2", "3", "4", "5"};

int Amt;
%>



<% for(int j=0; j <5; j++)
{
if(Amount[j] != null)
{
Amt = Integer.parseInt(Amount[j]);
%>
<%= "value_" + NumStrings[j] %>" value=" <%= Amt %>">
<%
}
else
{
out.print("There was a bad parameter!!!");
}
}
%>
%>

   <<Less

Re: Form processing questions

Posted By:   Stephen_McConnell  
Posted On:   Wednesday, June 20, 2001 04:17 AM

I didn't see the second question, but one can use the static methods:


Integer.toString(int value);


or


String.valueOf(int value);


To do this... Check out the JavaDocs for each of these API's, you'll find tons of methods to convert Strings to primative data types and back...


Stephen McConnell

Re: Form processing questions

Posted By:   Uma_Varadharajan  
Posted On:   Tuesday, June 19, 2001 02:53 PM

http://www.htmlgoodies.com/primers/jsp/hgjsp_29.html


has info on client-side validation. i.e, form is validated before posting. I resorted to javascript validation of forms on the clientside since I ran into problems when using Java code

Re: Form processing questions

Posted By:   Krishna_Dev  
Posted On:   Tuesday, June 19, 2001 02:40 PM

The best way to do this check is


if (Amount[j].trim().length != 0)
{
//valid parameter
}
else
{
//Invalid Parameter
}


About the second question, I dont really understand what you are trying to accomplish. In any case, you could have avoided using the NumString array by just using the loop index variable(j).

Re: Form processing questions

Posted By:   Stephen_McConnell  
Posted On:   Tuesday, June 19, 2001 08:02 AM

Try

if((Amount[j] != null)&&!(Amount[j].equalsIgnoreCase("null")))



I have found that an empty value inside the "input" tag will return a string with "null" in it... when you use request.getParameter("") to get the value.


Stephen McConnell

About | Sitemap | Contact