Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Java Validation! Field should be Integer

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am receiving a value in int type java variable from jsp.I want to check it is int type or other.If it is not int value than it give error.
Exp for:
int pincode; // It should be integer ant it's length must be 6 digit.

Why can i check it is int or not and also length.

I do not want to use any Script.I am waning your reply........
Than's
Amit Jain
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Amit Indore:

Why can i check it is int or not and also length.

I do not want to use any Script.I am waning your reply........



Hi Amit,

Looks like these questions are contradicting. It seems you are using a web application and you want to validate the input what you get should be an integer value and having 6 digits.

If you don't prefer doing it via a client side scripting, you can always validate the value in the server side. But it involves a round-trip to the server and then back to the client in case of any error.

Does that help?
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

For validating whether the input value is of type int, I would usually wrap the int value into Integer object and work on that. Does this help?

Thanks
Ravi
 
Amit Indore
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Raghavan Muthu:


Hi Amit,

Looks like these questions are contradicting. It seems you are using a web application and you want to validate the input what you get should be an integer value and having 6 digits.

If you don't prefer doing it via a client side scripting, you can always validate the value in the server side. But it involves a round-trip to the server and then back to the client in case of any error.

Does that help?



Thank's Sir,

Actually i am using struts1.2 and i want to define server side integer validation. like field name: Pin code/Zip,mobile number.
Sir, how can i check whole value of int variable is number and how can i get
its length.
 
Amit Indore
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Schandha Ravi:
Hi,

For validating whether the input value is of type int, I would usually wrap the int value into Integer object and work on that. Does this help?

Thanks
Ravi



Thank's

I try it.
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please try this code for Integer validation

public class RegularExpression_Examples {


public static boolean validateNumber(String number) {
return number.matches("^\\d*$");
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter Number: ");
String number = input.nextLine();
while (validateNumber(number) != true) {
System.out.print("Invalid PhoneNumber!");
System.out.println();
System.out.print("Enter Phone Number: ");
number = input.nextLine();
}

}
}
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic