Skip to content

Commit

Permalink
add general phone number validation per country
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimsn98 committed Jul 1, 2021
1 parent 0d13817 commit b607fb1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
8 changes: 1 addition & 7 deletions lib/src/main/java/me/ibrahimsn/lib/PhoneNumberKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.graphics.drawable.Drawable
import android.text.InputFilter
import android.text.InputType
import android.util.Patterns
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.google.android.material.textfield.TextInputLayout
Expand Down Expand Up @@ -268,10 +267,6 @@ class PhoneNumberKit(private val context: Context) {
return null
}

/**
* Provides country code
*/

/**
* Provides country for given country iso2
*/
Expand All @@ -286,7 +281,6 @@ class PhoneNumberKit(private val context: Context) {

private fun validate(number: CharSequence?): Boolean {
if (number == null) return false
return !number.isNullOrEmpty()
&& Patterns.PHONE.matcher(number).find()
return core.validateNumber(number.toString(), country?.iso2)
}
}
11 changes: 10 additions & 1 deletion lib/src/main/java/me/ibrahimsn/lib/core/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import me.ibrahimsn.lib.util.prependPlus
import me.ibrahimsn.lib.util.startsWithPlus
import java.util.*

class Core(context: Context) {
internal class Core(context: Context) {

private var phoneUtil: PhoneNumberUtil = PhoneNumberUtil.createInstance(context)

Expand Down Expand Up @@ -50,4 +50,13 @@ class Core(context: Context) {
null
}
}

fun validateNumber(number: String?, countryCode: String?): Boolean {
return try {
val p = parsePhoneNumber(number, countryCode)
return phoneUtil.isValidNumber(p)
} catch (e: Exception) {
false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package me.ibrahimsn.lib.util
import android.text.Editable
import android.text.TextWatcher

abstract class PhoneNumberTextWatcher : TextWatcher {
internal abstract class PhoneNumberTextWatcher : TextWatcher {

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
// no-op
Expand Down
10 changes: 0 additions & 10 deletions lib/src/main/java/me/ibrahimsn/lib/util/PhoneNumberValidator.kt

This file was deleted.

0 comments on commit b607fb1

Please sign in to comment.