Tuesday, February 19, 2019

Edit text field check.. email cannot be empty.

public static boolean emptyChecker(EditText[] editTexts, Activity activity) {
    /**     * checking input field for empty or not     */    int totalLength = 0;
    for (int i = 0; i < editTexts.length; i++) {
        EditText editText = editTexts[i];
        if (editText.getText().toString().trim().equals("")) {
            Toast.makeText(activity, editTexts[i].getTag().toString() + " " + activity.getString(R.string.is_empty), Toast.LENGTH_SHORT).show();
            break;
        } else {
            if (editText.getTag().toString().toLowerCase().equals(activity.getString(R.string.email).toLowerCase())) {
                String email = editText.getText().toString().trim();
                String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
                if (!email.matches(emailPattern)) {
                    Toast.makeText(activity, activity.getString(R.string.invalid), Toast.LENGTH_SHORT).show();
                    break;
                } else {
                    totalLength++;
                }
            } else {
                totalLength++;
            }

        }
    }
    //true means all okay    //false means at least one is empty    return totalLength == editTexts.length;
}