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
Printable View
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:
Code :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':
orCode :int inputAsString = 0; try{ inputAsString = Integer.parseInt(input); }catch ( NumberFormatException nfe ){ //deal with invalid number }
Code :if ( !input.matches("[0-9]+") ){//you can change this depending upon expeced input...negatives, decimals, length, etc... //deal with invalid number }
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.
Code :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
Thanks literallyjer, copeg and Json’s. Hai Json’s, I understand your point...
Thanks for your info...
Regards,