Source of ValidateInput.java


  1: // Fig. 29.20: ValidateInput.java
  2: // Validate user information using regular expressions.

  4: public class ValidateInput  
  5: {
  6:    // validate first name
  7:    public static boolean validateFirstName( String firstName )
  8:    {
  9:       return firstName.matches( "[A-Z][a-zA-Z]*" );
 10:    } // end method validateFirstName

 12:    // validate last name
 13:    public static boolean validateLastName( String lastName )
 14:    {
 15:       return lastName.matches( "[a-zA-z]+([ '-][a-zA-Z]+)*" );
 16:    } // end method validateLastName

 18:    // validate address
 19:    public static boolean validateAddress( String address )
 20:    {
 21:       return address.matches( 
 22:          "\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" );
 23:    } // end method validateAddress

 25:    // validate city
 26:    public static boolean validateCity( String city )
 27:    {
 28:       return city.matches( "([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" );
 29:    } // end method validateCity

 31:    // validate state
 32:    public static boolean validateState( String state )
 33:    {
 34:       return state.matches( "([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" ) ;
 35:    } // end method validateState

 37:    // validate zip
 38:    public static boolean validateZip( String zip )
 39:    {
 40:       return zip.matches( "\\d{5}" );
 41:    } // end method validateZip

 43:    // validate phone
 44:    public static boolean validatePhone( String phone )
 45:    {
 46:       return phone.matches( "[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}" );
 47:    } // end method validatePhone
 48: } // end class ValidateInput

 50: /*
 51:  **************************************************************************
 52:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 53:  * Pearson Education, Inc. All Rights Reserved.                           *
 54:  *                                                                        *
 55:  * DISCLAIMER: The authors and publisher of this book have used their     *
 56:  * best efforts in preparing the book. These efforts include the          *
 57:  * development, research, and testing of the theories and programs        *
 58:  * to determine their effectiveness. The authors and publisher make       *
 59:  * no warranty of any kind, expressed or implied, with regard to these    *
 60:  * programs or to the documentation contained in these books. The authors *
 61:  * and publisher shall not be liable in any event for incidental or       *
 62:  * consequential damages in connection with, or arising out of, the       *
 63:  * furnishing, performance, or use of these programs.                     *
 64:  **************************************************************************
 65: */