DEV Community

SEO - NT Tech Innovation
SEO - NT Tech Innovation

Posted on Edited on

Validating Bangladeshi Mobile Numbers (01X-XXXXXXXX) With One Regex in JavaScript

Almost every Bangladeshi web form asks for a mobile number. It's used for bKash payments, OTPs and delivery calls. Yet many forms reject valid numbers because someone typed +880 or added a dash.

One normalizer and one regex cover it.

The format

  • Local format is 11 digits and starts with 01.
  • The third digit is 3 to 9, depending on the operator.
  • The international format swaps the leading 0 for +880.

Normalize first

function normalizeBD(input) {
  let d = String(input).replace(/\D/g, '');
  if (d.startsWith('880')) d = d.slice(2);        // 8801... becomes 01...
  else if (d.length === 10 && d.startsWith('1')) d = '0' + d;
  return /^01[3-9]\d{8}$/.test(d) ? d : null;
}

console.log(normalizeBD('+880 1712-345678')); // 01712345678
console.log(normalizeBD('01712345678'));      // 01712345678
console.log(normalizeBD('1712345678'));       // 01712345678
console.log(normalizeBD('0121234567'));       // null
Enter fullscreen mode Exit fullscreen mode

Store one format

Save the 11-digit local version or the +880 version. Pick one and stick with it, so lookups never miss because of formatting.

const toE164 = (local) => '+88' + local;
console.log(toE164('01712345678')); // +8801712345678
Enter fullscreen mode Exit fullscreen mode

Form details that matter

  • Use input type tel and inputmode numeric.
  • Allow spaces and dashes while typing. Clean them yourself.
  • Write the error in plain words, in Bangla too if your users prefer it.

Don't use the number as proof of payment

A mobile number isn't a payment confirmation. For bKash, ask for the transaction ID and match it on the server.

Context

This matters most on sites where the whole flow runs on a phone, from sign-up to top-up. SMM panels are a good example. The best SMM panel in Bangladesh comparison from SMM Booster BD gives a sense of how many local services work this way.

Top comments (0)