Java Data Validation Class

I was working on a data validation this afternoon, and I thought I would share the class I wrote.

I found a good starting point at this blog and extended some of the methods.

This class uses the Java Date and Calendar constructs, as well as regular expressions to parse and match strings to validate several types of contact information.

The class is as follows:


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.widget.EditText;

public class DataValidation //
{
public static boolean isAfterCurrentDate(String dateString) //
{

//pass in a date string of the format MM/dd/yyyy

SimpleDateFormat sfd = new SimpleDateFormat("MM/dd/yyyy");

Date expDate = null;
try //
{
expDate = sfd.parse(dateString);
} //
catch (ParseException e) //
{
e.printStackTrace();
}
Date currentDate = Calendar.getInstance().getTime();

return expDate.after(currentDate);
}

public static Boolean isBeforeCurrentDate(String dateString) //
{

// pass in a date string of the format MM/dd/yyyy

SimpleDateFormat sfd = new SimpleDateFormat("MM/dd/yyyy");

Date expDate = null;
try //
{
expDate = sfd.parse(dateString);
} //
catch (ParseException e) //
{
e.printStackTrace();
}
Date currentDate = Calendar.getInstance().getTime();

return expDate.before(currentDate);
}

public static boolean isValidSSN(String ssn) //
{
/*
* SSN format xxx-xx-xxxx, xxxxxxxxx, xxx-xxxxxx; xxxxx-xxxx: ^\\d{3}:
* Starts with three numeric digits. [- ]?: Followed by an optional "-"
* \\d{2}: Two numeric digits after the optional "-" [- ]?: May contain
* an optional second "-" character. \\d{4}: ends with four numeric
* digits.
*
* Examples: 879-89-8989; 869878789 etc.
*/

String expression = "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$";
CharSequence inputStr = ssn;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
return matcher.matches();
}

public static boolean isValidZipCode(String zip) //
{
/*
* valid zip codes: 12345; 12345-1234 a group of 5 or a group of 5 then
* 4 with an optional dash
*/

String expression = "^\\d{5}[- ]?\\d{4}$|^\\d{5}";
CharSequence inputStr = zip;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
return matcher.matches();
}

public static boolean isValidStateCode(String stateCode) //
{
/*
* Valid state codes: MI; mi; Mi; mI any two alphabetical characters,
* upper or lowercase
*/

String expression = "^[a-zA-Z]{2}";
CharSequence inputStr = stateCode;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
return matcher.matches();
}

public static boolean isValidCreditCardNumber(String cardNumber) //
{
/*
* Credit card formats: 1234123412341234; 1234-1234-1234-1234 or any
* combination of dashes in groups of 4
*/

String expression = "^\\d{4}[- ]?\\d{4}[- ]?\\d{4}[- ]?\\d{4}?";
CharSequence inputStr = cardNumber;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
return matcher.matches();
}

public static boolean isPhoneNumberValid(String phoneNumber) //
{
/*
* Phone Number formats: (nnn)nnn-nnnn; nnnnnnnnnn; nnn-nnn-nnnn ^\\(? :
* May start with an option "(" . (\\d{3}): Followed by 3 digits. \\)? :
* May have an optional ")" [- ]? : May have an optional "-" after the
* first 3 digits or after optional ) character. (\\d{3}) : Followed by
* 3 digits. [- ]? : May have another optional "-" after numeric digits.
* (\\d{4})$ : ends with four digits.
*
* Examples: Matches following phone numbers: (123)456-7890,
* 123-456-7890, 1234567890, (123)-456-7890
*/
// Initialize reg ex for phone number.
String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
CharSequence inputStr = phoneNumber;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
return matcher.matches();
}

public static boolean isValidEmailAddress(String emailAddress) //
{
String expression = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = emailAddress;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
return matcher.matches();
}
}

If anyone has any questions on any of this code, feel free to contact me and I'll answer any questions I can, or post a question to my Stack Overflow account

© 2011 Cody Herring. Drupal theme by Kiwi Themes.