Skip to content

Instantly share code, notes, and snippets.

@msomu

msomu/ValidateUtils.java

Last active Dec 2, 2015
Embed
What would you like to do?
My Validate Utils
import android.app.Activity;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Somasundaram Mahesh on 12/1/2015.
*/
public class ValidateUtils {
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
private static final String MOBILE_PATTERN = "^(?:0091|\\+91|0|)[7-9][0-9]{9}$";
private static final String NAME_PATTERN = "^.*[a-zA-Z]+.*";
private static final String TAG = ValidateUtils.class.getSimpleName();
public static boolean ValidEmailId(String Email) {
return Email.matches(EMAIL_PATTERN);
}
public static boolean checkMobileNoFormat(String mobileNo) {
String mobile = mobileNo.replace(" ", "");
mobile = mobile.replace("-", "");
Pattern mobileNoPattern = Pattern.compile(MOBILE_PATTERN);
Matcher mat = mobileNoPattern.matcher(mobile);
Log.d(TAG, "Mobile Number" + mobileNo + " Validation result :" + mat.matches());
return mat.matches();
}
public static boolean checkIsName(String mobileNo) {
Pattern mobileNoPattern = Pattern.compile(NAME_PATTERN);
Matcher mat = mobileNoPattern.matcher(mobileNo);
return mat.matches();
}
public static boolean checkIsPassword(String password) {
return !(password.trim().isEmpty() || !(password.length() >= 6));
}
private static void requestFocus(View view, Activity activity) {
if (view.requestFocus()) {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
public static boolean validateEditText(Activity activity, TextInputLayout textInputLayout, EditText editText, String errorMessage, boolean isPassed) {
if (!isPassed) {
textInputLayout.setError(errorMessage);
requestFocus(editText, activity);
return false;
} else {
textInputLayout.setErrorEnabled(false);
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment