Utilities to Validate things

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;
}
}

view raw
ValidateUtils.java
hosted with ❤ by GitHub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s