ukrainian mobile phone number regex in javascript

Ukrainian mobile phone numbers can potentially start with a variety of prefixes, but they all have a total length of 12 digits. Here is a regular expression that matches Ukrainian mobile phone numbers:

index.tsx
const ukrainianMobileRegex = /^((\+38){0,1}0{0,1}[369]{1}\d{8})$/;
67 chars
2 lines

Explanation of the regex:

  • ^ - Start of string
  • ( - Start of capturing group
  • (\+38){0,1} - Matches the optional '+38' international prefix for Ukrainian numbers
  • 0{0,1} - Matches the optional leading zero in the local phone number format (XXX-XX-XX)
  • [369]{1} - Matches the first digit of the phone number, which can only be 3, 6, or 9
  • \d{8} - Matches the remaining 8 digits of the phone number, which can be any digit from 0 to 9
  • ) - End of capturing group
  • $ - End of string

Here's an example usage of this regex to validate a Ukrainian mobile phone number:

index.tsx
const phoneNumber = "+380501234567";
if (ukrainianMobileRegex.test(phoneNumber)) {
  console.log("Valid Ukrainian mobile phone number");
} else {
  console.log("Invalid Ukrainian mobile phone number");
}
204 chars
7 lines

gistlibby LogSnag