Hai all,
I have one doubt in my project. I want to validate input should be a number in server side... if u knows this, give the solution for that...
Kind Regards
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hai all,
I have one doubt in my project. I want to validate input should be a number in server side... if u knows this, give the solution for that...
Kind Regards
Are you using servlets?
What type of number are you expecting the input to be? If you're expecting an integer, you can do this:
Object input = new Integer(2); if (input instanceof Integer) // the input is an integer else // the input is something else
Assuming your input is a String entitled 'input':
orint inputAsString = 0; try{ inputAsString = Integer.parseInt(input); }catch ( NumberFormatException nfe ){ //deal with invalid number }
if ( !input.matches("[0-9]+") ){//you can change this depending upon expeced input...negatives, decimals, length, etc... //deal with invalid number }
Last edited by copeg; October 26th, 2009 at 11:16 AM.
Oh nooooes, here we go.
This will not work on floating point numbers like float or double but it will work for integers and below.
public static boolean isDigits(final String input) { if (input == null) { return false; } else if ("".equals(input)) { return false; } else { for (int i = 0; i < input.length(); ++i) { if (!Character.isDigit(input.charAt(i))) { return false; } } } return true; }
However if I was you I'd check out Lang - Home
// Json
Last edited by Json; October 27th, 2009 at 03:46 AM.
Thanks literallyjer, copeg and Json’s. Hai Json’s, I understand your point...
Thanks for your info...
Regards,