All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.convergence_platform.common.validations.AlphaNumericalCharactersOnlyValidator Maven / Gradle / Ivy

Go to download

Holds the common functionality needed by all Convergence Platform-based services written in Java.

The newest version!
package io.convergence_platform.common.validations;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class AlphaNumericalCharactersOnlyValidator implements ConstraintValidator {
    public static void pathVariable(String value, String varName) {
        boolean valid = true;
        if ("".equals(value)) {
            valid = false;
        } else {
            valid = validate(value);
        }

        ValidationHelper.raiseValidationErrorForPathVariable(valid, "alphanum", varName);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return validate(value);
    }

    private static boolean validate(String value) {
        if (value == null) {
            return false;
        }

        String allowed = "qwertyuiopasdfghjklzxcvbnm1234567890";
        value = value.toLowerCase();
        for (int i = 0; i < value.length(); i++) {
            String ch = String.valueOf(value.charAt(i));
            if (!allowed.contains(ch)) {
                return false;
            }
        }

        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy