user0809
user0809

Reputation: 11

Applying validation on attribute in Hybris

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

Answers (1)

mkysoft
mkysoft

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

Related Questions