Validation Problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Validation Problem

  1. #1
    Join Date
    Feb 2006
    Posts
    14

    Validation Problem

    Code:
    	public void actionPerformed( ActionEvent event )
    	{
    		if ( event.getSource() == moneyIn ) // if Money In button is pressed
    		{
    			//read in From Date and To Date from user as a string
    
    			fromDate = JOptionPane.showInputDialog( "Enter From Date" );
    			do 
    			{
    				JOptionPane.showMessageDialog(null, "From Date must be in the format dd/MM/yy", "Validation Error", JOptionPane.WARNING_MESSAGE);
    				fromDate = JOptionPane.showInputDialog( "Enter From Date" );	
    			} 
    			while (!fromDate.matches("^((0?[1-9])|(1\\d)|(2\\d)|(3[0-1]))\\/((0?[1-9])|(1[0-2]))\\/(\\d\\d)"));
    			
    			toDate = JOptionPane.showInputDialog( "Enter To Date" );
    			do 
    			{
    				JOptionPane.showMessageDialog(null, "From Date must be in the format dd/MM/yy", "Validation Error", JOptionPane.WARNING_MESSAGE);
    				toDate = JOptionPane.showInputDialog( "Enter To Date" );
    			} 
    			while (!toDate.matches("^((0?[1-9])|(1\\d)|(2\\d)|(3[0-1]))\\/((0?[1-9])|(1[0-2]))\\/(\\d\\d)"));
    			
    			// create JTextArea to display output
    			JTextArea outputTextArea = new JTextArea();
    				
    			// set first line of text in outputTextArea
    			outputTextArea.setText( "From Date\t To Date\t Amount\n" );
    				
    			// append one line of text to outputTextArea
    			outputTextArea.append( fromDate + "\t" + toDate + "\t" + amount  + "\n" );
    			
    			// display results
    			JOptionPane.showMessageDialog( null, outputTextArea,
    			"Money In", JOptionPane.INFORMATION_MESSAGE );
    		}
    I am trying to get this bit of testing validation code working. The two do while statements are meant to check what the user inputs and if its wrong, show an error and ask them to re-enter it. The current method only works if the user enters it wrong the first time, but always brings up the error if its right the first time.

    What can I do to overcome this problem?

  2. #2
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    You should try to use a boolean helper for checking:

    Code:
    ...
    if ( event.getSource() == moneyIn ) // if Money In button is pressed
    {
      boolean isOK = false;
      while (!isOK){
        fromDate = JOptionPane.showInputDialog( "Enter From Date" );
        if (fromDate.matches("^((0?[1-9])|(1\\d)|(2\\d)|(3[0-1]))\\/((0?[1-9])|(1[0-2]))\\/(\\d\\d)"))
          isOK=true;
        else JOptionPane.showMessageDialog(null, "From Date must be in the format dd/MM/yy", "Validation Error", JOptionPane.WARNING_MESSAGE);
      } // end while
     ...
    same for all other dialogs.

Similar Threads

  1. form validation problem
    By LucyG in forum ASP.NET
    Replies: 1
    Last Post: 02-09-2002, 08:45 PM
  2. Replies: 0
    Last Post: 10-24-2001, 05:09 PM
  3. Validation
    By Marcia in forum Java
    Replies: 1
    Last Post: 09-14-2000, 12:04 PM
  4. Replies: 0
    Last Post: 06-23-2000, 03:17 PM
  5. Problem with CryptoAPI and JCE
    By Jason Bock in forum VB Classic
    Replies: 0
    Last Post: 03-21-2000, 07:48 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center