Sorry for being new to programming. Apparently I'm expected to know everything about the topic before posting here. I've only been doing this 3 weeks, learning as I go.
I've got a field limited to 5 characters I'm trying to validate. The first character must be a letter, and the following 4 characters must be numbers. Regex is like Greek to me at this point, so I'm having trouble. I've been able to get the first character validated, but I'm stumped on the remaining 4. Here's my code:
if (carID.substring(0, 1).matches("[0-9]")) {
showDataFormatError();
break;
} else {
if (carID.substring(1, 5).matches("[a-zA-Z]")) {
showDataFormatError();
break;
}
}
Updating to demonstrate my horrible coding at this point.
if (carID.length() < 5) {
showDataLengthError();
break;
} else {
if (carID.matches("^[a-zA-Z][0-9]{4}$")) {
showDataFormatError();
break;
} else {
if (carYearString.length() < 0) {
showDateLengthError();
break;
} else {
try {
int carYear = Integer.parseInt(carYearString);
int currentYear = Calendar.getInstance().get(
Calendar.YEAR);
// etc.
carID.matches("^[A-Za-z]\\d{4}$").showDataFormatError()when the data is correct. You needif (!carID.matches ...