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.

Results 1 to 5 of 5

Thread: Validate in server side..

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Validate in server side..

    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


  2. #2
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Validate in server side..

    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

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Validate in server side..

    Assuming your input is a String entitled 'input':
    int inputAsString = 0;
    try{
        inputAsString = Integer.parseInt(input);
    }catch ( NumberFormatException nfe ){
        //deal with invalid number
    }
    or
    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.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Validate in server side..

    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.

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Validate in server side..

    Thanks literallyjer, copeg and Json’s. Hai Json’s, I understand your point...
    Thanks for your info...

    Regards,

Similar Threads

  1. Replies: 3
    Last Post: December 22nd, 2011, 09:46 AM
  2. Replies: 1
    Last Post: July 28th, 2009, 02:15 AM
  3. Java program to open jsp page on client side instead of server side
    By khanshakil in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 06:26 AM