• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Server side Integer fields validation in Struts 1.1

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Amit ,

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

the simple search gives me thousands of example cliclk
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic