Skip to content

Instantly share code, notes, and snippets.

@erfanegtfi
Created April 13, 2018 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erfanegtfi/6f2f3b51c1996f15082e0a2271c6e74c to your computer and use it in GitHub Desktop.
Save erfanegtfi/6f2f3b51c1996f15082e0a2271c6e74c to your computer and use it in GitHub Desktop.
valid email, mobile, price, ssn ...
public class Validator {
public static boolean isValidEmailAddress(String email) {
String emailRegex;
String stricterFilterString = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
String laxString = ".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
if (true) {
emailRegex = stricterFilterString;
} else {
emailRegex = laxString;
}
return Pattern.compile(emailRegex).matcher(email).matches();
}
public static boolean isMobileNumber(String mobileNumber) {
return Pattern.compile("09(1[0-9]|3[1-9]|2[1-9])-?[0-9]{3}-?[0-9]{4}").matcher(mobileNumber).matches();
}
public static boolean IsPassV2(String Text) {
return Pattern.compile("^[a-zA-Z0-9]{4,10}$").matcher(Text).find();
}
public static boolean IsUnicode(String Text) {
return !Pattern.compile("^[a-zA-Z0-9]{4,10}$").matcher(Text).matches();
}
public static boolean IsEnglish(String Text) {
return Pattern.compile("^[a-zA-Z0-9]{4,10}$").matcher(Text).matches();
}
public static boolean checkPriceValid(String price){
Pattern pattern = Pattern.compile("(^[1-9][0-9]{1,8})$");
Matcher matcher = pattern.matcher(price);
return matcher.find();
}
public static boolean checkCountValid(String price){
Pattern pattern = Pattern.compile("^[1-9][0-9]*$");
Matcher matcher = pattern.matcher(price);
return matcher.matches();
}
public static boolean isSSN(String input) {
input = input.replace("!", "");
if (!input.matches("^\\d{10}$"))
return false;
int check = Integer.parseInt(input.charAt(9) + "");
int sum = 0;
for (int i = 0; i < 9; ++i)
sum += Integer.parseInt(input.charAt(i) + "") * (10 - i);
sum %= 11;
return (sum < 2 && check == sum) || (sum >= 2 && check + sum == 11);
}
public static boolean checkPriceValid(String price) {
price = price.replace(",", "");
Pattern pattern = Pattern.compile("(^[1-9][0-9]{3,10})$");
Matcher matcher = pattern.matcher(price);
return matcher.find();
}
public static boolean checkInputPersianValid(String price) {
Pattern pattern = Pattern.compile("^[ضصثقفغعهخحجچپگکمنتالبیسشظطزرژذدئويك. آ ُِ]+$");//1
// Pattern pattern = Pattern.compile("^[\\u0636\\u0635\\u062b\\u0642\\u0641\\u063a\\u0639\\u0647\\u062e\\u062d\\u062c\\u0686\\u067e\\u06af\\u06a9\\u0645\\u0646\\u062a\\u0627\\u0644\\u0628\\u06cc\\u0633\\u0634\\u0638\\u0637\\u0632\\u0631\\u0698\\u0630\\u062f\\u0626\\u0648\\u064a\\u0643\\u002e \\u0622 ]+$");//2
Matcher matcher = pattern.matcher(price.trim());
// boolean valid = false;
// if (price != null)
// for (int i = 0; i < price.length(); i++) {//3
// int unicode = (int) price.charAt(i);
// if ((unicode <= 1740 && unicode >= 1570)|| unicode==32 || unicode==46)
// valid = true;
// else
// valid = false;
// }
return matcher.find();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment