Skip to content

Instantly share code, notes, and snippets.

@Abdallah-Abdelazim
Last active March 12, 2021 00:42
Show Gist options
  • Save Abdallah-Abdelazim/e873f82bbdad60cd0f90fc23f60fdd15 to your computer and use it in GitHub Desktop.
Save Abdallah-Abdelazim/e873f82bbdad60cd0f90fc23f60fdd15 to your computer and use it in GitHub Desktop.
Various utility functions to validate user input.
package com.example.util;
import android.text.TextUtils;
import android.util.Patterns;
import androidx.annotation.NonNull;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Various utility functions to validate user input.
*
* @author Abdallah Abdelazim
*/
public final class InputValidationUtils {
private InputValidationUtils() {
// Prevent instantiation
}
/**
* Checks that the passed string is a valid email address.
*
* @param email the string to validate.
* @return true if {@code s} contains a valid email address, false otherwise.
*/
public static boolean isEmailValid(String email) {
return (!TextUtils.isEmpty(email) && Patterns.EMAIL_ADDRESS.matcher(email).matches());
}
/**
* Checks if a given string contains any special characters.
* <br>
* A special character is any character that is not A-Z, a-z, 0-9 or any whitespace.
*
* @param s the string to validate.
* @return true if {@code s} contains one or more special characters,
* false if it contains no special characters at all.
*/
public static boolean containsSpecialChar(String s) {
return isRegexMatch("[^a-zA-Z0-9\\s]", s);
}
/**
* Checks if a given string starts with a special character.
* <br>
* A special character is any character that is not A-Z, a-z, 0-9 or any whitespace.
*
* @param s the string to validate.
* @return true if the string starts with a special character while ignoring all
* leading whitespaces, false otherwise.
*/
public static boolean isStartingWithSpecialChar(String s) {
return isRegexMatch("^\\s*[^a-zA-Z0-9\\s](.|\\s)*$", s);
}
/**
* Checks if a given string contains only special characters.
* <br>
* A special character is any character that is not A-Z, a-z, 0-9 or any whitespace.
*
* @param s the string to validate.
* @return true if all the non-whitespace characters in the string are special characters,
* false otherwise.
*/
public static boolean isAllSpecialChars(String s) {
return isRegexMatch("^(\\s*[^a-zA-Z0-9\\s]\\s*)+$", s);
}
/**
* Checks if a given string starts with an invalid character.
* <br>
* An invalid character is any punctuation or control character.
*
* @param s the string to validate.
* @return true if the string starts with an invalid character while ignoring all
* leading whitespaces, false otherwise.
*/
public static boolean isStartingWithInvalidChar(String s) {
return isRegexMatch("^\\s*[\\p{Punct}\\p{Cntrl}](.|\\s)*$", s);
}
/**
* Checks if a given string contains only invalid characters.
* <br>
* An invalid character is any punctuation or control character.
*
* @param s the string to validate.
* @return true if all the non-whitespace characters in the string are invalid characters,
* false otherwise.
*/
public static boolean isAllInvalidChars(String s) {
return isRegexMatch("^(\\s*[\\p{Punct}\\p{Cntrl}]\\s*)+$", s);
}
private static boolean isRegexMatch(@NonNull String regex, String s) {
if (s == null || s.isEmpty())
return false;
Pattern pattern = Pattern.compile(Objects.requireNonNull(regex,
"The passed regex string cannot be null"));
Matcher matcher = pattern.matcher(s);
return matcher.matches();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment