Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FSADT1-982: Discrepancy in email validation #600

Merged
merged 5 commits into from
Nov 9, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@
import ca.bc.gov.app.dto.client.LegalTypeEnum;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.validation.Errors;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ClientValidationUtils {

private static final EmailValidator emailValidator = EmailValidator.getInstance();
private static final String EMAIL_REGEX = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$";
private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);

public static void validateEmail(String email, String field, Errors errors) {
if (StringUtils.isBlank(email)) {
errors.rejectValue(field, "You must enter an email address");
return;
}

if (!emailValidator.isValid(email)) {
Matcher emailMatcher = EMAIL_PATTERN.matcher(email);
if (!emailMatcher.matches()) {
errors.rejectValue(field, "You must enter an email address in a valid format. "
+ "For example: [email protected]");
}
Expand Down
Loading