Monday, July 25, 2016

Currency and Phone Number Format Check?


public String currencyFormatCheck(String location, int currencyValue) {
if (location.equalsIgnoreCase("US")) {
Locale currentLocale = Locale.US;
Double currencyAmount = new Double(currencyValue);
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
return currencyFormatter.format(currencyAmount);
} else if (location.equalsIgnoreCase("UK")) {
Locale currentLocale = Locale.UK;
Double currencyAmount = new Double(currencyValue);
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
return currencyFormatter.format(currencyAmount);
}
return null;
}
 
private static boolean phoneNumberFormatCheck(String phoneNo) {
// validate phone numbers of format "1234567890"
if (phoneNo.matches("\\d{10}"))
return true;
// validating phone number with -, . or spaces
else if (phoneNo.matches("\\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4}"))
return true;
// validating phone number with extension length from 3 to 5
else if (phoneNo.matches("\\d{3}-\\d{3}-\\d{4}\\s(x|(ext))\\d{3,5}"))
return true;
// validating phone number where area code is in braces ()
else if (phoneNo.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}"))
return true;
// return false if nothing matches the input
else
return false;
}

//Example '(866) 825-3227' then you can use below regular expression

public static boolean validatePhone(String phone) {
 String phonePattern = "\\([0-9]{3}) [0-9]{3}\\-[0-9]{4}";
 // e.g. (866) 825-3227
 Pattern pattern = Pattern.compile(phonePattern);
 Matcher matcher = pattern.matcher(phone);
 return matcher.matches();
}

No comments:

Post a Comment