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 Mike Lewis 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
0
0
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 "";
}
%>
I wouldike to validate this select box andd return back to same page if user doesn't select anythg but even after writing this code it doesnot get back.i want to validate at jsp end not servlet end using jsp function not java scripts..IS somethg wrong with the above code

Do let me know is it right...
Thanks,



 
Hi,

You can validate only when the page is submitted if not you have to use javascript to validate on the client side.

example of how you do it after the page is submit.
Code:
 <%--
  Created by IntelliJ IDEA.
  User: vreddy
  Date: Jul 27, 2005
  Time: 4:14:48 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.IOException"%>
<%
    if(null != request.getParameter("select")){
        validateSelect(request,response);
    }
%>
<%!
  public void validateSelect(HttpServletRequest request, HttpServletResponse response){
   String selectValue = request.getParameter("select");
   if((null != selectValue) && ("".equals(selectValue))){
     request.setAttribute("Error","Error Message : Need to Select a Value");
    }else{
       try {
           request.getRequestDispatcher("testjs.jsp").forward(request, response);
       } catch (ServletException e) {
           System.out.println(e.toString());

       } catch (IOException e) {
           System.out.println(e.toString());

       }
   }
  }
%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Select</title>
  </head>
  <body>
    <form>
      <table cellspacing="3" cellpadding="2" border="0" width="100%">
        <tr>
          <td width="31%">Select
          </td>
          <td width="69%">
            <select name="select">
              <option value=""></option>
              <option value="Value">Caption</option>
              <option value="Value2">Caption2</option>
            </select> <font color="red">
             <%
                if(null!= request.getAttribute("Error")){
                    out.print(request.getAttribute("Error").toString());
                }
             %></font>
          </td>
        </tr>
        <tr>
          <td width="31%">
            <input type="submit" value="Submit"/>
          </td>
          <td width="69%">&nbsp;</td>
        </tr>
      </table>
    </form>
  </body>
</html>

Cheers
Venu
 
The code u send is for the next page after this page is submitted if i would have to check the select list values on submit using the same jsp and load it back if no bvalue selected is it possible?
thanks
 
Hi,

It is not possible with JSP you have to use JavaScript if you need to validate the values on onSubmit of the page.

Cheers
Venu
 
Hi,

The above code does a similar thing but its after submitting the page. First it will validate does the user has selected from the select list if not it will load the same page but not the values, for which you have to write extra code. If the user has selected the value it will dispatch the request to "testjs.jsp" which could be your next page. But its only after you submit the page.

Cheers
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top