This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Struts and the fly likes Server side Integer fields validation in Struts 1.1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Server side Integer fields validation in Struts 1.1" Watch "Server side Integer fields validation in Struts 1.1" New topic
Author

Server side Integer fields validation in Struts 1.1

Amit Indore
Ranch Hand

Joined: Jun 20, 2008
Posts: 31
Hi All,
This is my first Project in Struts.

I am fessing a problem. I do not know how can i give server side validation
on integer fields like Pin code/Zip , Mobile No. should be digit and specify the length.

Please help my.

I am wetting your response.

Thank's
Amit Jain
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

HI Amit ,

welcome to javaranch ,
This validation you can perform on client side also !

the simple search gives me thousands of example cliclk


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
John Robert
Ranch Hand

Joined: Jun 17, 2008
Posts: 39
Hi, you can use a validation frame work, or you can validate it in the 'validate' method of the Actionform. For your senario the validation in the ActionForm can be as simple as checking if the input is not empty/ if its a number/ if its of specific legenth.
[ June 23, 2008: Message edited by: John Robert ]
Amit Indore
Ranch Hand

Joined: Jun 20, 2008
Posts: 31
Originally posted by John Robert:
Hi, you can use a validation frame work, or you can validate it in the 'validate' method of the Actionform. For your senario the validation in the ActionForm can be as simple as checking if the input is not empty/ if its a number/ if its of specific legenth.

[ June 23, 2008: Message edited by: John Robert ]


Yes i know but i do not want to define it's required fields and how can check
whole value of variable is number And how can get length of int variable and How can define it's length.
Amit Indore
Ranch Hand

Joined: Jun 20, 2008
Posts: 31
Originally posted by Sagar Rohankar:
HI Amit ,

welcome to javaranch ,
This validation you can perform on client side also !

the simple search gives me thousands of example cliclk


Tahk's Sir,

I know but my sir want to server side validation.He does not want to use script.

sir, can i use server side and client side validation together.means in same
jsp page.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

sir, can i use server side and client side validation together.means in same
jsp page


There is no good reason using both the type of validation in same JSP page,,
and one major difference is server side validation is done by java classes which resides on your server and client side validation you can perform on client side like, in JSP/HTML page using scripts !!

So based on you requirement , you either choose server side or client side validation.

Now if your action form bean is extends ActionForm , then you can override validate() method and perform validation on form field like ,pincode , telephone nos, and return specific ActionError ,(See API for details)..

Or post your action form bean code here !
[ July 01, 2008: Message edited by: Sagar Rohankar ]
Amit Indore
Ranch Hand

Joined: Jun 20, 2008
Posts: 31
Originally posted by Sagar Rohankar:


There is no good reason using both the type of validation in same JSP page,,
and one major difference is server side validation is done by java classes which resides on your server and client side validation you can perform on client side like, in JSP/HTML page using scripts !!

So based on you requirement , you either choose server side or client side validation.

Now if your action form bean is extends ActionForm , then you can override validate() method and perform validation on form field like ,pincode , telephone nos, and return specific ActionError ,(See API for details)..

Or post your action form bean code here !

[ July 01, 2008: Message edited by: Sagar Rohankar ]


Hello Sir,
I have done it.

I am sending my code,so please check it and suggest me that is right way or not.
1)

I put that code in Form Been Error Validation method.
if(!(pincode ==null || pincode.length()<1))
{ System.out.println("pincode:"+pincode);
if(pincode.length()!=6)

errors.add("pincode", new ActionError("error.invalid.pincode"));
}

2)I put that code in Action class


String pinno = ((CreateUserForm) form).getPincode();

int pincode=0;
try{
pincode=Integer.parseInt(pinno);
System.out.println("Pincode :: "+pincode);
}catch(Exception e){
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalid.pincode"));
saveErrors(request,errors);
System.out.println("Exception cought :: Exception :: " + e);
return (new ActionForward(mapping.getInput()));
}

Sir, it is working fine But i want to know that i am right or not.

After your suggestion i will apply that process to all value in my project.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

First of all put your code snippet in

tag.



Here if you are using validate() method of ActionForm , then there is no need to check pincode in Action class ..

A word of suggestion ..

Why don`t you use client side validation using Struts validation framework for Pincode , etc..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Server side Integer fields validation in Struts 1.1