Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jsp validation function

Status
Not open for further replies.

kondakindi

IS-IT--Management
Apr 18, 2005
31
US
Hi,
I have a select box in the jsp page i would like to check if the user has selected any item if not it should not proceeed and reload the same page with error stating you have to select .
i have wriiten a function to check null values it doesn't work..
<select name="q_value">
<option value="q_val" " <%= isMultipleSelected(request, "q_value" , q_val)%> ">One
</select>


funtion is :


<%! public static String[] getFormElementValues(HttpServletRequest request, String formElementName)
{
String [] values = null;
if (!((request ==null) || (formElementName ==null)||(request.getParameter(formElementName) ==null)))
{
values = (String[])request.getParameterValues(formElementName);
}

return values;
}

public static String isMultipleSelected (HttpServletRequest request, String param, String testValue)
{
if (getFormElementValues(request, param) != null )
{
String[] values = getFormElementValues(request, param);
for(int i =0;i<values.length;i++)
{
String Value = (String)values;
if(Value.equals(testValue))
return " selected ";
}
}
return "";
}
%>


Do let me know is it right...
Thanks,
 
There's a JSP forum for questions like this.

Anyway

what does your funtion do?
what error do you get?
what are the actual and expected results?

Cheers,

Dian
 
<%
response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
String selectVal = request.getParameter("s01");
System.out.println(selectVal);
int choice = 0;
if (selectVal!=null)
{
selectVal = selectVal.replaceAll("c","");
choice = Integer.parseInt(selectVal);
}
%>
<html>
<head>
</head>
<body>
<form action="form.jsp">
<select name="s01">
<option value="" >select one</option>
<option value="c1" <% if (choice==1) {%>selected<%}%> >one</option>
<option value="c2" <% if (choice==2) {%>selected<%}%> >two</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
// this is a simple jsp, you should consider the result from the select box is null after the jsp is first load
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top