Android examples for java.util.regex:Number Pattern
is string a valid Phone Number
import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.text.TextUtils; import java.io.DataOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main{ public static boolean isPhoneNumberValid(String phoneNumber) { boolean isValid = false; String expression = "^//(?(//d{3})//)?[- ]?(//d{3})[- ]?(//d{5})$"; String expression2 = "^//(?(//d{3})//)?[- ]?(//d{4})[- ]?(//d{4})$"; CharSequence inputStr = phoneNumber; Pattern pattern = Pattern.compile(expression); Matcher matcher = pattern.matcher(inputStr); Pattern pattern2 = Pattern.compile(expression2); Matcher matcher2 = pattern2.matcher(inputStr); if (matcher.matches() || matcher2.matches()) { isValid = true;/* w ww . j av a 2 s .co m*/ } return isValid; } }