Skip to content

Instantly share code, notes, and snippets.

@KalpeshTalkar
Last active October 2, 2022 09:42
Show Gist options
  • Select an option

  • Save KalpeshTalkar/e6a50efd08a3d5d142e47da559936bb7 to your computer and use it in GitHub Desktop.

Select an option

Save KalpeshTalkar/e6a50efd08a3d5d142e47da559936bb7 to your computer and use it in GitHub Desktop.

Revisions

  1. KalpeshTalkar revised this gist Mar 23, 2020. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Validator.kt
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,6 @@
    // For support: https://gist.github.com/KalpeshTalkar/e6a50efd08a3d5d142e47da559936bb7
    //

    package com.infini.itap.Utils

    import android.support.design.widget.TextInputLayout
    import android.util.Patterns
    import android.widget.EditText
  2. KalpeshTalkar revised this gist Mar 23, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Validator.kt
    Original file line number Diff line number Diff line change
    @@ -177,7 +177,7 @@ class Validator {
    setError(data, error)
    }

    return true
    return valid
    }

    /**
  3. KalpeshTalkar revised this gist Jan 25, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Validator.kt
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@
    // See the License for the specific language governing permissions and
    // limitations under the License.
    //
    // For support: https://gist.github.com/KalpeshTalkar/eebb69a3fee4fc8ffcf4eac25757b435
    // For support: https://gist.github.com/KalpeshTalkar/e6a50efd08a3d5d142e47da559936bb7
    //

    package com.infini.itap.Utils
  4. KalpeshTalkar created this gist Jan 25, 2018.
    200 changes: 200 additions & 0 deletions Validator.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,200 @@
    //
    // Copyright © 2018 Kalpesh Talkar. All rights reserved.
    //
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    //
    // For support: https://gist.github.com/KalpeshTalkar/eebb69a3fee4fc8ffcf4eac25757b435
    //

    package com.infini.itap.Utils

    import android.support.design.widget.TextInputLayout
    import android.util.Patterns
    import android.widget.EditText
    import java.util.regex.Pattern

    /**
    * Created by Kalpesh on 23/01/18.
    *
    * This class is used to validate data like email, phone, password policy, etc.
    * It also sets error to the EditText or TextInputLayout of the EditText if needed.
    */
    class Validator {

    companion object {

    // Default validation messages
    private val PASSWORD_POLICY = """Password should be minimum 8 characters long,
    |should contain at least one capital letter,
    |at least one small letter,
    |at least one number and
    |at least one special character among ~!@#$%^&*()-_=+|[]{};:'\",<.>/?""".trimMargin()

    private val NAME_VALIDATION_MSG = "Enter a valid name"
    private val EMAIL_VALIDATION_MSG = "Enter a valid email address"
    private val PHONE_VALIDATION_MSG = "Enter a valid phone number"

    /**
    * Retrieve string data from the parameter.
    * @param data - can be EditText or String
    * @return - String extracted from EditText or data if its data type is Strin.
    */
    private fun getText(data: Any): String {
    var str = ""
    if (data is EditText) {
    str = data.text.toString()
    } else if (data is String) {
    str = data
    }
    return str
    }

    /**
    * Checks if the name is valid.
    * @param data - can be EditText or String
    * @param updateUI - if true and if data is EditText, the function sets error to the EditText or its TextInputLayout
    * @return - true if the name is valid.
    */
    fun isValidName(data: Any, updateUI: Boolean = true): Boolean {
    val str = getText(data)
    val valid = str.trim().length > 2

    // Set error if required
    if (updateUI) {
    val error: String? = if (valid) null else NAME_VALIDATION_MSG
    setError(data, error)
    }

    return valid
    }

    /**
    * Checks if the email is valid.
    * @param data - can be EditText or String
    * @param updateUI - if true and if data is EditText, the function sets error to the EditText or its TextInputLayout
    * @return - true if the email is valid.
    */
    fun isValidEmail(data: Any, updateUI: Boolean = true): Boolean {
    val str = getText(data)
    val valid = Patterns.EMAIL_ADDRESS.matcher(str).matches()

    // Set error if required
    if (updateUI) {
    val error: String? = if (valid) null else EMAIL_VALIDATION_MSG
    setError(data, error)
    }

    return valid
    }

    /**
    * Checks if the phone is valid.
    * @param data - can be EditText or String
    * @param updateUI - if true and if data is EditText, the function sets error to the EditText or its TextInputLayout
    * @return - true if the phone is valid.
    */
    fun isValidPhone(data: Any, updateUI: Boolean = true): Boolean {
    val str = getText(data)
    val valid = Patterns.PHONE.matcher(str).matches()

    // Set error if required
    if (updateUI) {
    val error: String? = if (valid) null else PHONE_VALIDATION_MSG
    setError(data, error)
    }

    return valid
    }

    /**
    * Checks if the password is valid as per the following password policy.
    * Password should be minimum minimum 8 characters long.
    * Password should contain at least one number.
    * Password should contain at least one capital letter.
    * Password should contain at least one small letter.
    * Password should contain at least one special character.
    * Allowed special characters: "~!@#$%^&*()-_=+|/,."';:{}[]<>?"
    *
    * @param data - can be EditText or String
    * @param updateUI - if true and if data is EditText, the function sets error to the EditText or its TextInputLayout
    * @return - true if the password is valid as per the password policy.
    */
    fun isValidPassword(data: Any, updateUI: Boolean = true): Boolean {
    val str = getText(data)
    var valid = true

    // Password policy check
    // Password should be minimum minimum 8 characters long
    if (str.length < 8) {
    valid = false
    }
    // Password should contain at least one number
    var exp = ".*[0-9].*"
    var pattern = Pattern.compile(exp, Pattern.CASE_INSENSITIVE)
    var matcher = pattern.matcher(str)
    if (!matcher.matches()) {
    valid = false
    }

    // Password should contain at least one capital letter
    exp = ".*[A-Z].*"
    pattern = Pattern.compile(exp)
    matcher = pattern.matcher(str)
    if (!matcher.matches()) {
    valid = false
    }

    // Password should contain at least one small letter
    exp = ".*[a-z].*"
    pattern = Pattern.compile(exp)
    matcher = pattern.matcher(str)
    if (!matcher.matches()) {
    valid = false
    }

    // Password should contain at least one special character
    // Allowed special characters : "~!@#$%^&*()-_=+|/,."';:{}[]<>?"
    exp = ".*[~!@#\$%\\^&*()\\-_=+\\|\\[{\\]};:'\",<.>/?].*"
    pattern = Pattern.compile(exp)
    matcher = pattern.matcher(str)
    if (!matcher.matches()) {
    valid = false
    }

    // Set error if required
    if (updateUI) {
    val error: String? = if (valid) null else PASSWORD_POLICY
    setError(data, error)
    }

    return true
    }

    /**
    * Sets error on EditText or TextInputLayout of the EditText.
    * @param data - Should be EditText
    * @param error - Message to be shown as error, can be null if no error is to be set
    */
    private fun setError(data: Any, error: String?) {
    if (data is EditText) {
    if (data.parent.parent is TextInputLayout) {
    (data.parent.parent as TextInputLayout).setError(error)
    } else {
    data.setError(error)
    }
    }
    }

    }

    }