Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.text.TextUtils; import android.util.Log; import android.util.Patterns; import android.widget.EditText; import android.widget.Toast; import java.util.regex.Pattern; public class Main { private static final boolean YES = true; private static final boolean NO = false; public static boolean mobileNumberValidation(EditText editText) { if (nonEmpty(editText)) { String mobileNumber = removeBlankSpace(editText.getText().toString().trim()); return Patterns.PHONE.matcher(mobileNumber).matches(); } else { Log.d("SERI_PAR->Error", "edit text object is null"); return NO; } } public static boolean mobileNumberValidation(Context context, EditText editText, String msg) { if (mobileNumberValidation(editText)) { return YES; } else { showToast(context, msg); return NO; } } public static boolean mobileNumberValidation(EditText editText, Pattern pattern) { if (nonEmpty(editText)) { String mobileNumber = removeBlankSpace(editText.getText().toString()); return pattern.matcher(mobileNumber).matches(); } else { return NO; } } public static boolean mobileNumberValidation(Context context, EditText editText, Pattern pattern, String msg) { if (mobileNumberValidation(editText, pattern)) { return YES; } else { showToast(context, msg); return NO; } } public static boolean mobileNumberValidation(EditText editText, int minLength, int maxLength) { if (minLength > 0 && maxLength > 0 && nonEmpty(editText)) { String mobileNumber = removeBlankSpace(editText.getText().toString().trim()); return mobileNumber.length() >= minLength && mobileNumber.length() <= maxLength; } else { return NO; } } public static boolean mobileNumberValidation(Context context, EditText editText, int minLength, int maxLength, String msg) { if (mobileNumberValidation(editText, minLength, maxLength)) { return YES; } else { showToast(context, msg); return NO; } } public static boolean nonEmpty(EditText editText) { if (editText != null && !(TextUtils.isEmpty(editText.getText().toString().trim()))) { return YES; } else { Log.d("SERI_PAR->Error", "edit text object is null"); return NO; } } public static boolean nonEmpty(Context context, EditText editText, String msg) { if (nonEmpty(editText)) { return YES; } else { showToast(context, msg); return NO; } } public static String removeBlankSpace(String value) { value = value.replace(" ", ""); return value; } public static void showToast(Context context, String msg) { Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } }