Created
April 11, 2016 08:27
-
-
Save sheerazam/c790a526ff4a4a5183c4d1d72f726f50 to your computer and use it in GitHub Desktop.
This file contains 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
public class Validation { | |
//Regular Expression | |
private static final String EMAIL_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; | |
private static final String PHONE_REGEX = "\\d{3}-\\d{7}"; | |
private static final String MOBILE_REGX = "\\d{4}-\\d{7}"; | |
// Error Messages | |
private static final String REQUIRED_MSG = "required"; | |
private static final String EMAIL_MSG = "invalid email"; | |
private static final String PHONE_MSG = "###-#######"; | |
private static final String MOBILE_MSG = "####-#######"; | |
private static final String PASSWORD_MSG = "password not matching"; | |
public static boolean isMatching(EditText editText, EditText editText1, boolean required) { | |
return check(editText, editText1, PASSWORD_MSG, required); | |
} | |
private static boolean check(EditText editText, EditText editText1, | |
String passwordMsg, boolean required) { | |
String text = editText.getText().toString().trim(); | |
String text1 = editText1.getText().toString().trim(); | |
editText.setError(null); | |
editText1.setError(null); | |
if ( required && isTextnotEmpty(editText) && isTextnotEmpty(editText1) ) return false; | |
if(required && !text1.equals(text)){ | |
editText1.setError(passwordMsg); | |
return false; | |
} | |
return true; | |
} | |
/**Checks for Validity of Email Address | |
* @param email the input email address | |
* @param required pass true if it is required, false if not | |
* @return true if valid email address, false if not | |
* | |
* */ | |
public static boolean isValidEmailAddress(EditText email, boolean required) { | |
return isValid(email, EMAIL_REGEX, EMAIL_MSG, required); | |
} | |
/**Checks for Validity of Phone Number | |
* @param phone_no the input phone number | |
* @param required pass true if it is required, false if not | |
* @return true if valid phone number, false if not | |
* | |
* */ | |
public static boolean isValidPhoneNumber(EditText phone_no, boolean required) { | |
return isValid(phone_no, PHONE_REGEX, PHONE_MSG, required); | |
} | |
public static boolean isValidMobileNumber(EditText phone_no, boolean required) { | |
return isValid(phone_no, MOBILE_REGX, MOBILE_MSG, required); | |
} | |
/**Checks for Validity of Any Input Field with the regular expression in it | |
* @param mytext the input text field to check | |
* @param regex the regular expression which can appear in it | |
* @param errMsg the error message to set if field is not valid | |
* @param required pass true if it is required, false if not | |
* @return true if valid input field, false if not | |
* | |
* */ | |
public static boolean isValid(EditText mytext, String regex, String errMsg, boolean required) { | |
String text = mytext.getText().toString().trim(); | |
// clearing the error, if it was previously set by some other values | |
mytext.setError(null); | |
// text required and editText is blank, so return false | |
if ( required && !isTextnotEmpty(mytext) ) return false; | |
// pattern doesn't match so returning false | |
if (required && !Pattern.matches(regex, text)) { | |
mytext.setError(errMsg); | |
return false; | |
}; | |
return true; | |
} | |
/**Checks whether input field is empty of not | |
* @param mytext the input text field to check | |
* @return true if input field is not empty, false if not | |
* */ | |
public static boolean isTextnotEmpty(EditText mytext) { | |
String text = mytext.getText().toString().trim(); | |
mytext.setError(null); | |
if (text.length() == 0) { | |
mytext.setError(REQUIRED_MSG); | |
return false; | |
} | |
return true; | |
} | |
public static boolean isTextNotTooLong(EditText mytext, String message) { | |
String text = mytext.getText().toString().trim(); | |
mytext.setError(null); | |
if (text.length() > 20) { | |
mytext.setError(message); | |
return false; | |
} | |
return true; | |
} | |
public static boolean doesBtnContainDate(Button btn, String message, Context context) { | |
String text = btn.getText().toString().trim(); | |
btn.setError(null); | |
if ( !text.contains(",") ) { | |
btn.setError(message); | |
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment