Results 1 to 7 of 7

Thread: Java Validation

  1. #1
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Java Validation

    How do you validate that a number entered by a user is only 5 integers?

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Java Validation

    input.matches("\\d{5}")
    or do you mean max 5 integers?
    input.matches("\\d{1,5}")
    if input is a string
    Pattern (Java Platform SE 6)

    if not, use mathematics :D

  3. #3
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Validation

    I want to verify that the number being entered by the user IS 5 numbers long...this is the code I was working with:
    Java Code:
    	userInput = Integer.valueOf(JOptionPane.showInputDialog("Please Enter A 5 digit number"));
    	
    	convertedInput = Integer.toString(userInput).length();
    	
    	if (convertedInput != 5)
    	{
    	//Run this code
    	}
    	else
    	{
    	//Run this code
    	}
    I keep getting a compile error of error can not find symbol

  4. #4
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Validation

    This isn't working either:
    Java Code:
    	if (String.valueOf(userInput).trim().length() != 5);
    	{		
    		JOptionPane.showMessageDialog(null, "Please enter a valid 5 digit integer");
    	}

    Let me word it a little better
    That's throwing an error on every input that I try?!
    Last edited by Rahim2312; 05-07-2012 at 06:59 PM.

  5. #5
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Java Validation

    Please post the error message! Did you import the right packages?

  6. #6
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Validation

    These are the packages I have:
    import java.lang.Math;
    import javax.swing.JOptionPane;
    import java.awt.*;
    import javax.swing.*;
    import java.util.Scanner;

    And the error I get is regardless of what number is input, it gives me the message "Please enter a valid 5 digit integer"

  7. #7
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Validation

    OMG I had just been staring at the code for too long....I got this error figured out, I'll post back if I run into another :)

Similar Threads

  1. how to do validation using java swing?
    By Vignesh Karthick in forum AWT / Swing
    Replies: 3
    Last Post: 01-25-2013, 01:38 PM
  2. Help!! Validation for java
    By peppy99 in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2011, 12:55 PM
  3. Help!! Validation for java
    By peppy99 in forum New To Java
    Replies: 14
    Last Post: 01-07-2011, 10:33 AM
  4. Java Form Validation
    By Lovie in forum Java Servlet
    Replies: 7
    Last Post: 09-22-2009, 05:52 PM
  5. java validation?
    By lockmac in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •