Monday, November 24, 2014

Date Validation Using Regex (While onTextChanged) in EditText

A regular expression is a string of characters that describes a character sequence. This general description, called a pattern, can then be used to find matches in other character sequences. Regular expressions can specify wildcard characters, sets of characters, and various quantifiers. Thus, you can specify a regular expression that represents a general form that can match several different specific character sequences. There are two classes that support regular expression processing: Pattern and Matcher.

The Following code only for US Date Format (MM-DD-YYYY)


public boolean validate(final String validateDateStr) {

try {

if (validateDateStr.length() != 0) {

if (validateDateStr.matches("(0?[1-9]|1[012])")) {

return true;

} else if (validateDateStr.matches("(0?[1-9]|1[012])[-]")) {

return true;

} else if (validateDateStr.matches("(0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01])")) {
String[] StartDate = validateDateStr.toString().split("-");
String month = StartDate[0];
String day = StartDate[1];
if (day.equals("31") && (month.equals("4") || month.equals("6") || month.equals("9") || month.equals("11") || month.equals("04") || month.equals("06") || month.equals("09"))) {
return false; // only 1,3,5,7,8,10,12 has 31 days
}

else if (month.equals("2") || month.equals("02")) {
if (day.equals("30") || day.equals("31")) {
return false;
} else {
return true;
}

} else {
return true;
}
} else if (validateDateStr.matches(("(0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01])[-]")) || (validateDateStr.matches("(0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01])[-]([2])")) || (validateDateStr.matches("(0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01])[-]([2][01])")) || (validateDateStr.matches("(0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01])[-]([2][0][0-9])"))) {
String[] StartDate = validateDateStr.toString().split("-");
String month = StartDate[0];
String day = StartDate[1];
if (day.equals("31") && (month.equals("4") || month.equals("6") || month.equals("9") || month.equals("11") || month.equals("04") || month.equals("06") || month.equals("09"))) {
return false; // only 1,3,5,7,8,10,12 has 31 days
}
return true;
}

else if (validateDateStr.matches("(0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01])[-]([2][0][0-9][0-9])")) {

String[] StartDate = validateDateStr.toString().split("-");
String month = StartDate[0];
String day = StartDate[1];
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
Date myDate = null;
try {
myDate = dateFormat.parse(validateDateStr.toString());
} catch (ParseException e) {
e.printStackTrace();
}

SimpleDateFormat DateFormatyyyy = new SimpleDateFormat("yyyy");
String year = DateFormatyyyy.format(myDate);
if (day.equals("31") && (month.equals("4") || month.equals("6") || month.equals("9") || month.equals("11") || month.equals("04") || month.equals("06") || month.equals("09"))) {
return false; // only 1,3,5,7,8,10,12 has 31 days
} else if (month.equals("2") || month.equals("02")) {
if (Integer.parseInt(year) % 4 == 0) {
if (day.equals("30") || day.equals("31")) {
return false;
} else {
return true;
}
} else {
if (day.equals("29") || day.equals("30") || day.equals("31")) {
return false;
} else {
return true;
}
}
} else {
return true;
}

}

else {
return false;
}
}

} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {

}
return false;
}

No comments:

Post a Comment