Last active
July 30, 2018 09:45
-
-
Save nontravis/ba81b17710487b7cc1aefad5f1e4fb84 to your computer and use it in GitHub Desktop.
ValidateLogic.kt
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
fun @receiver:ValidateString Int.isValidate(): Boolean = this and VALIDATE == VALIDATE | |
fun @receiver:ValidateString Int.isInvalidate(): Boolean = this and INVALIDATE == INVALIDATE | |
fun String?.validateCommon(): Int { | |
if (this.isNullOrEmpty()) return INVALIDATE_EMPTY | |
return VALIDATE | |
} | |
fun @receiver:ValidateString Int.isInvalidateEmail(): Boolean = this and INVALIDATE_EMAIL == INVALIDATE_EMAIL | |
fun String?.validateEmail(): Int { | |
if (this.validateCommon().isInvalidate()) return this.validateCommon() | |
if (!Pattern.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}\\@[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}(\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25})+").matcher(this).matches()) return INVALIDATE_EMAIL_FORMAT | |
if (this?.toLowerCase()?.contains("gmail") == false) return INVALIDATE_EMAIL_NOT_GMAIL | |
return VALIDATE | |
} | |
fun @receiver:ValidateString Int.isInvalidateMobileNumber(): Boolean = this and INVALIDATE_MOBILE_NUMBER == INVALIDATE_MOBILE_NUMBER | |
fun String?.validateMobileNumber(): Int { | |
if (this.validateCommon().isInvalidate()) return this.validateCommon() | |
if (this != null && this.length > 1 && this[0] != '0') return INVALIDATE_MOBILE_NUMBER_FIRST_NOT_ZERO | |
if (this != null && this.length < 9) return INVALIDATE_MOBILE_NUMBER_LEN_LESS_NINE | |
if (!Pattern.matches("\\d+", this)) return INVALIDATE_MOBILE_NUMBER_NOT_ALL_NUMBER | |
return VALIDATE | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment