Created
July 27, 2015 11:29
-
-
Save vaibhav-jani/8d834dfde5d8252362bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.utils; | |
import android.content.Context; | |
import android.text.TextUtils; | |
import android.util.Patterns; | |
import android.widget.EditText; | |
/** | |
* Created by darshan on 2/4/15. | |
*/ | |
public class FieldsValidator { | |
public static void showValidationMessage(String message, EditText editText, Context context) { | |
if(editText != null) { | |
//editText.setError(message); | |
editText.requestFocus(); | |
} | |
Notify.toast(message, context); | |
} | |
public static boolean validateUsername(EditText editText) { | |
Context context = editText.getContext(); | |
String emptyField = context.getString(R.string.empty_username); | |
String invalidField = context.getString(R.string.invalid_username); | |
String text = editText.getText().toString().trim(); | |
if(TextUtils.isEmpty(text)) { | |
showValidationMessage(emptyField, editText, context); | |
return false; | |
} /*else if(isValidEmail(text)) { | |
showValidationMessage(invalidField, editText, context); | |
return false; | |
}*/ | |
return true; | |
} | |
public static boolean validatePassword(EditText editText) { | |
Context context = editText.getContext(); | |
String emptyField = context.getString(R.string.empty_password); | |
String invalidField = context.getString(R.string.invalid_password); | |
String text = editText.getText().toString().trim(); | |
if(TextUtils.isEmpty(text)) { | |
showValidationMessage(emptyField, editText, context); | |
return false; | |
} /*else if(isValidEmail(text)) { | |
showValidationMessage(invalidField, editText, context); | |
return false; | |
}*/ | |
return true; | |
} | |
public static boolean validateEmail(EditText editText) { | |
Context context = editText.getContext(); | |
String emptyField = context.getString(R.string.empty_email); | |
String invalidField = context.getString(R.string.invalid_email); | |
String text = editText.getText().toString().trim(); | |
if(TextUtils.isEmpty(text)) { | |
showValidationMessage(emptyField, editText, context); | |
return false; | |
} else if(isValidEmail(text)) { | |
showValidationMessage(invalidField, editText, context); | |
return false; | |
} | |
return true; | |
} | |
public static boolean isValidPhoneNumber(CharSequence target) { | |
if(target == null || TextUtils.isEmpty(target)) { | |
return false; | |
} else { | |
return Patterns.PHONE.matcher(target).matches(); | |
} | |
} | |
public static boolean isValidEmail(CharSequence target) { | |
if(target == null || TextUtils.isEmpty(target)) { | |
return false; | |
} else { | |
return Patterns.EMAIL_ADDRESS.matcher(target).matches(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment