| Author |
Java Validation! Field should be Integer
|
Amit Indore
Ranch Hand
Joined: Jun 20, 2008
Posts: 30
|
|
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
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
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?
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
Schandha Ravi
Ranch Hand
Joined: Oct 20, 2007
Posts: 167
|
|
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
|
Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
|
 |
Amit Indore
Ranch Hand
Joined: Jun 20, 2008
Posts: 30
|
|
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
Joined: Jun 20, 2008
Posts: 30
|
|
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.
|
 |
Sri Sudha
Greenhorn
Joined: Jun 07, 2011
Posts: 1
|
|
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();
}
}
}
|
 |
 |
|
|
subject: Java Validation! Field should be Integer
|
|
|