Results 1 to 4 of 4
  1. #1
    obdi is offline Member
    Join Date
    Jul 2008
    Location
    CA
    Posts
    15
    Rep Power
    0

    Default Need Help On Entry Validation

    Project is to make a data entry program, with FirstName, MiddleInitial, LastName, and SSN. Then it comes down to the entry validation part.

    I think the codes i wrote are correct, tested on single entry worked great. But when i put 4 validations together(FirstName, MiddleInitial, LastName, and SSN), there is always some problems.

    I only know if, else if and switch(I tried swtich but it totally didn't work for me), this code was my last try on else if, and this way of coding always ends up poping the alpha - numeric entry error msg, for the FirstName entry. And I'm totally out of ideas of how to write the validation code. Please help!

    part of the code
    Java Code:
    //Validating First Name Filed
    if(TOne.getText().length() != 0 && Determinator == BufferOne.length())
    {
    	for(k = 0; k < BufferOne.length(); k++)
    	{
    		for(m = 0; m < 26; m++)
    		{
    			if(BufferOne.charAt(k) == Alpha.charAt(m))
    			{
    				Determinator++;
    			}
    		}
    	}
    }
    else if(TOne.getText().length() == 0)
    {
    	JOptionPane.showMessageDialog(null, "First Name Field REQUIRED!");
    	TOne.requestFocus();
    }
    else if(Determinator != BufferOne.length())
    {
    	JOptionPane.showMessageDialog(null, "INVALID CHARACTER at First Name Field!" +
    					"\nPlease Enter A Valid FIRST NAME!"+
    					"\nEntry Should be Alpha Character A-Z!");
    	TOne.setText("");
    	TOne.requestFocus();	
    }
    Code with The Action()
    Java Code:
    private class ClickWrite implements ActionListener
    {
    	public void actionPerformed(ActionEvent e)
    	{
    		String MeUp, BufferOne, BufferTwo, BufferThree, BufferFour;
    		String Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    		String Numeric = "0123456789";
    		int k,m,Determinator;
    		try
    		{
    			FileWriter WriteStream = new FileWriter("c:/random/IOFile.txt",true);
    			PrintWriter Write      = new PrintWriter(WriteStream);
    			
    			BufferOne   = TOne.getText().toUpperCase();
    			BufferTwo   = TTwo.getText().toUpperCase();
    			BufferThree = TThree.getText().toUpperCase();
    			BufferFour  = TFour.getText();
    			
    			Determinator = 0;
    			
    			//Validating First Name Filed
    			if(TOne.getText().length() != 0 && Determinator == BufferOne.length())
    			{
    				for(k = 0; k < BufferOne.length(); k++)
    				{
    					for(m = 0; m < 26; m++)
    					{
    						if(BufferOne.charAt(k) == Alpha.charAt(m))
    						{
    							Determinator++;
    						}
    					}
    				}
    			}
    			else if(TOne.getText().length() == 0)
    			{
    				JOptionPane.showMessageDialog(null, "First Name Field REQUIRED!");
    				TOne.requestFocus();
    			}
    			else if(Determinator != BufferOne.length())
    			{
    				JOptionPane.showMessageDialog(null, "INVALID CHARACTER at First Name Field!" +
    								"\nPlease Enter A Valid FIRST NAME!"+
    								"\nEntry Should be Alpha Character A-Z!");
    				TOne.setText("");
    				TOne.requestFocus();	
    			}
    			//Validating Middle Initial Field
    			else if(TTwo.getText().length() != 0 && Determinator == BufferTwo.length())
    			{
    				for(k = 0; k < BufferTwo.length(); k++)
    				{
    					for(m = 0; m < 26; m++)
    					{
    						if(BufferTwo.charAt(k) == Alpha.charAt(m))
    						{
    							Determinator++;
    						}
    					}
    				}
    			}
    			else if(TTwo.getText().length() == 0)
    			{
    				JOptionPane.showMessageDialog(null, "Middle Initial Field REQUIRED!");
    				TTwo.requestFocus();
    			}
    			else if(Determinator != BufferTwo.length() || Determinator > 1)
    			{
    				JOptionPane.showMessageDialog(null, "INVALID CHARACTER at Middle Initial Field!" +
    								"\nPlease Enter A Valid MIDDLE INITIAL!"+
    								"\nEntry Should be Alpha Character A-Z!");
    				TTwo.setText("");
    				TTwo.requestFocus();	
    			}
    			//Validating Last Name Field
    			else if(TThree.getText().length() != 0 && Determinator == BufferThree.length())
    			{
    				for(k = 0; k < BufferThree.length(); k++)
    				{
    					for(m = 0; m < 26; m++)
    					{
    						if(BufferThree.charAt(k) == Alpha.charAt(m))
    						{
    							Determinator++;
    						}
    					}
    				}
    			}
    			else if(TThree.getText().length() == 0)
    			{
    				JOptionPane.showMessageDialog(null, "Last Name Field REQUIRED!");
    				TThree.requestFocus();
    			}
    			else if(Determinator != BufferThree.length())
    			{
    				JOptionPane.showMessageDialog(null, "INVALID CHARACTER at Last Name Field!" +
    								"\nPlease Enter A Valid LAST NAME!"+
    								"\nEntry Should be Alpha Character A-Z!");
    				TThree.setText("");
    				TThree.requestFocus();	
    			}
    			//Validating SSN Field
    			else if(TFour.getText().length() != 0 && Determinator == BufferFour.length())
    			{
    				for(k = 0; k < BufferFour.length(); k++)
    				{
    					for(m = 0; m < 10; m++)
    					{
    						if(BufferFour.charAt(k) == Numeric.charAt(m))
    						{
    							Determinator++;
    						}
    					}
    				}
    			}
    			else if(TFour.getText().length() == 0)
    			{
    				JOptionPane.showMessageDialog(null, "SSN Field REQUIRED!");
    				TFour.requestFocus();
    			}
    			else if(Determinator != BufferFour.length())
    			{
    				JOptionPane.showMessageDialog(null, "INVALID CHARACTER at SSN Field!" +
    								"\nPlease Enter A Valid SSN!"+
    								"\nEntry Should be Numeric Numbers 0-9!");
    				TFour.setText("");
    				TFour.requestFocus();	
    			}
    			else
    			{
    				BufferThree = "";
    				BufferThree = TThree.getText().toLowerCase() + "                    ";
    				BufferThree = BufferThree.substring(0,20);
    				BufferThree = BufferThree.substring(0,1).toUpperCase() + BufferThree.substring(1,20);
    				MeUp        =  BufferThree;
    				
    				BufferOne = "";
    				BufferOne = TOne.getText().toLowerCase() + "               ";
    				BufferOne = BufferOne.substring(0,15);
    				BufferOne = BufferOne.substring(0,1).toUpperCase() + BufferOne.substring(1,15);
    				MeUp      = MeUp + BufferOne;
    				
    				BufferTwo = "";
    				BufferTwo = TTwo.getText().toUpperCase() + " ";
    				BufferTwo = BufferTwo.substring(0,1);
    				MeUp      = MeUp + BufferTwo;
    				
    				BufferFour = "";
    				BufferFour = TFour.getText() + "         ";
    				BufferFour = BufferFour.substring(0,9);
    				MeUp       = MeUp + BufferFour;
    				
    				Write.println(MeUp);
    				JOptionPane.showMessageDialog(null, "Record is written, c:/random/IOFile.txt");
    				WriteRecord.setEnabled(true);
    				AddRecord.setEnabled(true);
    			}
    			
    			Write.close();
    		}
    		catch(IOException WhatIsTheMatter)
    		{
    			System.out.println("IO Error = " + WhatIsTheMatter.getMessage());
    		}
    	}
    }
    If i create different Buttons that would work, like FirstName Button LastName Button i.e.
    But i'm wondering how do i do it in one button? and how can i write all of them under one action(), Thanks!!
    Last edited by obdi; 07-25-2008 at 04:45 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    obdi is offline Member
    Join Date
    Jul 2008
    Location
    CA
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Why don't you use nested if loops there. It's simple.
    Java Code:
    if(TOne.getText().length() == 0)
    {					
    	JOptionPane.showMessageDialog(null, "First Name Field REQUIRED!");
    	TOne.requestFocus();
    }
    else if(Determinator != BufferOne.length())
    {
    	JOptionPane.showMessageDialog(null, "INVALID CHARACTER at First Name Field!" +
    															"\nPlease Enter A Valid FIRST NAME!"+
    															"\nEntry Should be Alpha Character A-Z!");
    	TOne.setText("");
    	TOne.requestFocus();	
    }
    else if(Determinator == BufferOne.length() && TOne.getText().length() != 0)
    {
    	for(k = 0; k < BufferOne.length(); k++)
    	{
    		for(m = 0; m < 26; m++)
    		{
    			if(BufferOne.charAt(k) == Alpha.charAt(m))
    			{
    				Determinator++;
    			}
    		}
    	}
    }
    else if()
    else if()
    ...
    still having the same problem, keep displaying alpha - numeric error msg, or is it i'm not placing the codes right?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 0
    Last Post: 07-02-2008, 08:11 PM
  2. How to prevent duplicate username entry in database?
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 01-09-2008, 08:02 AM
  3. Duplicate entry in registration form!!!
    By anki1234 in forum Advanced Java
    Replies: 1
    Last Post: 01-04-2008, 08:15 PM
  4. need help checking monthlyRate entry
    By lowpro in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 05:15 AM
  5. javascript validation
    By yuchuang in forum New To Java
    Replies: 5
    Last Post: 05-14-2007, 03:38 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
  •