Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Validation! Field should be Integer

 
Amit Indore
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
 
Raghavan Muthu
Ranch Hand
Posts: 3381
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?
 
Schandha Ravi
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.
 
Sri Sudha
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();
}

}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic