/app/src/main/java/com/eventyay/organizer/utils/ValidateUtils.java
https://github.com/fossasia/open-event-orga-app · Java · 63 lines · 48 code · 13 blank · 2 comment · 3 complexity · 0d695270427e3359a60239a42bf3999c MD5 · raw file
- package com.eventyay.organizer.utils;
- import android.text.Editable;
- import android.text.TextUtils;
- import android.text.TextWatcher;
- import com.eventyay.organizer.common.Function;
- import com.google.android.material.textfield.TextInputLayout;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public final class ValidateUtils {
- private static final Pattern VALID_EMAIL_ADDRESS_REGEX =
- Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
- private static final Pattern VALID_URL_REGEX =
- Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
- private ValidateUtils() {
- }
- public static boolean validateEmail(String emailStr) {
- Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
- return matcher.find();
- }
- public static boolean validateUrl(String urlStr) {
- Matcher matcher = VALID_URL_REGEX.matcher(urlStr);
- return matcher.find();
- }
- public static void validate(TextInputLayout textInputLayout, Function<String, Boolean> validationReference, String errorResponse) {
- textInputLayout.getEditText().addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- // Nothing here
- }
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- if (validationReference.apply(charSequence.toString())) {
- textInputLayout.setError(null);
- textInputLayout.setErrorEnabled(false);
- } else {
- textInputLayout.setErrorEnabled(true);
- textInputLayout.setError(errorResponse);
- }
- if (TextUtils.isEmpty(charSequence)) {
- textInputLayout.setError(null);
- textInputLayout.setErrorEnabled(false);
- }
- }
- @Override
- public void afterTextChanged(Editable editable) {
- // Nothing here
- }
- });
- }
- }