Reputation: 11
I have created Pan attribute on registration page. I have to apply validation for it like 1-5 digits will be alphabets, 6-9 will be numeric and tenth digit will be alphabet.
I tried logic for length of Pan no it worked private void validatePan(final Errors errors, final String pan) {
if (StringUtils.isEmpty(pan))
{
errors.rejectValue("pan", "register.pan.invalid");
}
else if (StringUtils.length(pan) > 10 || StringUtils.length(pan) < 10)
{
errors.rejectValue("pan", "register.pan.invalid");
}
}
Please provide the next part of validation as per requirement.
Upvotes: 0
Views: 247
Reputation: 5758
You can use regex for single line of code like below:
final Pattern PATTERN_PAN = Pattern.compile("^[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}$");
if (!PATTERN_PAN.matcher(pan).matches()) {
errors.rejectValue("pan", "register.pan.invalid");
}
Upvotes: 1