Skip to main content
Phone numbers are hard: every country has its own lengths, prefixes and mobile ranges, and they change. The phone constraints rely on Google’s libphonenumber, so they live in their own module, and the core stays free of it.
The constraints are in the package io.github.soleworks.validity.phone:
Courier.kt

phone

phone(countryCode, areaCode, format) on String Checks that the string is a valid phone number, by the metadata of libphonenumber. Every argument is optional and narrows what passes: a number from another country, with another area code or written in another format fails.
  • countryCode: the country calling code the number must have, such as 55 or +55. With null, any country.
  • areaCode: the area code the number must have, such as 11. With null, any area code.
  • format: how the number must be written, a PhoneFormat. With null, any of them.
phone

mobilePhone

mobilePhone(countryCode, areaCode, format) on String Checks that the string is a valid mobile phone number, with the same arguments as phone. Numbers that can be mobile or fixed line, as in the United States, pass. mobilePhone

countryCode

countryCode() on String Checks that the string is a country calling code, such as 55 or +1, with or without the +. countryCode

areaCode

areaCode(countryCode) on String Checks that the string is an area code used in countryCode, such as 11 in Brazil. Where the area codes of a country have different lengths, any 1 to 5 digits pass. areaCode

Formats

PhoneFormat says how a number is written:

Messages

The messages of the module are in a block of their own: