user3428799
user3428799

Reputation: 21

How to apply validation for alphanumeric ID?

I have to provide validation to an ID field that is in alphanumeric order. ID starts with a letter, followed by 7 digits. How do I validate this in Java? Is it possible to achieve using a MySQL database?

For example, T1234567 should pass validation, and shows a correctly formatted ID field that I'd like to store.

Upvotes: 1

Views: 153

Answers (1)

Spencer Brett
Spencer Brett

Reputation: 236

Java Pattern library

You'll want to use this library to do any sort of pattern matching. I'm not sure about the structure of your application, but at its most basic level, this kind of validation based on a pattern should be done using regular expressions (regex). Feel free to do some research on the topic if you're not familiar with it.

The pattern you described would be captured by the following:

Pattern p = Pattern.compile("[A-Z|a-z]\\d{7}");
Matcher m = p.matcher(someID);
boolean validId = m.matches();

There are many ways to do this however.

Upvotes: 1

Related Questions