
Phone number parsing library based on Google’s Libphonenumber. Validates, normalizes, and formats phone numbers, with embedded metadata for efficient runtime performance.
Phone number parsing library for Kotlin Multiplatform. Based on Google’s Libphonenumber and PhoneNumberKit.
Add the dependency to your build.gradle.kts:
implementation("com.bayo-code:kphonenumber:0.11.0")This is available on Maven Central. Then create a KPhoneNumber object:
import com.bayocode.kphonenumber.KPhoneNumber
val kPhoneNumber = KPhoneNumber()All your interactions with KPhoneNumber will happen through this object.
To parse a string, use this function:
try {
val phoneNumber = kPhoneNumber.parse("+33 6 89 017383")
} catch (exception: PhoneNumberException) {
// handle exception
}This returns a PhoneNumber object that contains information about the parsed phone number. If the parsing fails, it throws a PhoneNumberException, so make sure to catch that.
You can also pass a specific region for the phone number:
try {
val phoneNumber = kPhoneNumber.parse("+33 6 89 017383", "GB")
} catch (exception: PhoneNumberException) {
// handle exception
}You can also disable validation, which ensures that the returned object is a valid phone number:
try {
val phoneNumber = kPhoneNumber.parse("+33 6 89 017383", "GB", true)
} catch (exception: PhoneNumberException) {
// handle exception
}Formatting the returned phone number is easy:
kPhoneNumber.format(phoneNumber, PhoneNumberFormat.E164) // +61236618300
kPhoneNumber.format(phoneNumber, PhoneNumberFormat.International) // +61 2 3661 8300
kPhoneNumber.format(phoneNumber, PhoneNumberFormat.National) // // (02) 3661 8300To get an As-you-type formatter to format incomplete phone numbers, you can use the following snippet:
val partialFormatter = kPhoneNumber.partialFormatter()
val formattedString = partialFormatter.formatPartial("+336895555") // +33 6 89 55 55PhoneNumberMetadata.json file in metadata-generator/metadata/ folder.metadata-generator module. This will re-generate the metadata and update it in the code.Phone number parsing library for Kotlin Multiplatform. Based on Google’s Libphonenumber and PhoneNumberKit.
Add the dependency to your build.gradle.kts:
implementation("com.bayo-code:kphonenumber:0.11.0")This is available on Maven Central. Then create a KPhoneNumber object:
import com.bayocode.kphonenumber.KPhoneNumber
val kPhoneNumber = KPhoneNumber()All your interactions with KPhoneNumber will happen through this object.
To parse a string, use this function:
try {
val phoneNumber = kPhoneNumber.parse("+33 6 89 017383")
} catch (exception: PhoneNumberException) {
// handle exception
}This returns a PhoneNumber object that contains information about the parsed phone number. If the parsing fails, it throws a PhoneNumberException, so make sure to catch that.
You can also pass a specific region for the phone number:
try {
val phoneNumber = kPhoneNumber.parse("+33 6 89 017383", "GB")
} catch (exception: PhoneNumberException) {
// handle exception
}You can also disable validation, which ensures that the returned object is a valid phone number:
try {
val phoneNumber = kPhoneNumber.parse("+33 6 89 017383", "GB", true)
} catch (exception: PhoneNumberException) {
// handle exception
}Formatting the returned phone number is easy:
kPhoneNumber.format(phoneNumber, PhoneNumberFormat.E164) // +61236618300
kPhoneNumber.format(phoneNumber, PhoneNumberFormat.International) // +61 2 3661 8300
kPhoneNumber.format(phoneNumber, PhoneNumberFormat.National) // // (02) 3661 8300To get an As-you-type formatter to format incomplete phone numbers, you can use the following snippet:
val partialFormatter = kPhoneNumber.partialFormatter()
val formattedString = partialFormatter.formatPartial("+336895555") // +33 6 89 55 55PhoneNumberMetadata.json file in metadata-generator/metadata/ folder.metadata-generator module. This will re-generate the metadata and update it in the code.