UK mobile number check
editThis page shows a small unit, UkNumber, that cleans up a UK phone number typed by a person and says which Ofcom block it belongs to. It is useful in a sign-up form, an import tool or an alert list: anything where "starts with 07" is not a good enough test for "can receive a text".
It compiles with Free Pascal 3.2.2 in objfpc mode, needs only the RTL, and has no platform-specific code.
Why "starts with 07" is not enough
editThe 07 block of the Ofcom numbering plan is shared by several kinds of number:
| Prefix | What it is | Sensible for SMS? |
|---|---|---|
| 071 to 075, 077 to 079 | standard mobile, 11 digits | yes |
| 07624 | Isle of Man mobile | yes, treated as mobile here |
| 070 | personal numbering (forwarding) | usually not |
| 076 except 07624 | radiopaging | no |
Outside 07, 01 and 02 are geographic landlines, 03 is non-geographic, 05 is corporate numbering (often VoIP), 08 is freephone or special rate and 09 is premium rate. People also type the same number in many ways: 07700 900123, +44 (0)7700 900123 and 0044 7700-900-123 are all the same mobile. The unit turns all of them into one national form first, so the prefix test only has to be written once.
The unit
editunit UkNumber;
{$mode objfpc}{$H+}
interface
type
TUkKind = (ukInvalid, ukStandardMobile, ukPersonal, ukPaging,
ukGeographic, ukNonGeographic, ukCorporate, ukSpecialRate, ukPremium);
{ Strip spaces, dashes, dots and brackets, drop the +44 / 0044 prefix and
the "(0)" trunk digit, and return the national form: a leading 0 and
nine or ten more digits. Returns '' when the text cannot be a UK number. }
function NationalForm(const AText: string): string;
{ "+447700900123", or '' when NationalForm fails. }
function InternationalForm(const AText: string): string;
{ Which Ofcom block the number is in. }
function Kind(const AText: string): TUkKind;
{ True only for 07 numbers that look like a normal 11 digit mobile. }
function IsStandardMobile(const AText: string): Boolean;
{ '' when the prefix looks fine for SMS, otherwise a short reason. }
function SmsRejectReason(const AText: string): string;
implementation
function NationalForm(const AText: string): string;
var
I: Integer;
Digits, Trimmed: string;
begin
Digits := '';
for I := 1 to Length(AText) do
case AText[I] of
'0'..'9': Digits := Digits + AText[I];
'+':
if Digits = '' then
Digits := '+'
else
Exit('');
' ', '-', '.', '(', ')': ; { separators are ignored }
else
Exit('');
end;
{ +44 7700 900123 and 0044 7700 900123 }
if Copy(Digits, 1, 3) = '+44' then
Trimmed := '0' + Copy(Digits, 4, MaxInt)
else if Copy(Digits, 1, 4) = '0044' then
Trimmed := '0' + Copy(Digits, 5, MaxInt)
else
Trimmed := Digits;
{ +44 (0)7700 900123 keeps a stray trunk 0 after the country code }
if (Copy(Trimmed, 1, 2) = '00') and (Pos('(0)', AText) > 0) then
Trimmed := Copy(Trimmed, 2, MaxInt);
if (Length(Trimmed) < 10) or (Length(Trimmed) > 11) then Exit('');
if Trimmed[1] <> '0' then Exit('');
if Trimmed[2] = '0' then Exit('');
Result := Trimmed;
end;
function InternationalForm(const AText: string): string;
var
N: string;
begin
N := NationalForm(AText);
if N = '' then
Result := ''
else
Result := '+44' + Copy(N, 2, MaxInt);
end;
function Kind(const AText: string): TUkKind;
var
N: string;
begin
N := NationalForm(AText);
if N = '' then Exit(ukInvalid);
case N[2] of
'1', '2': Result := ukGeographic;
'3': Result := ukNonGeographic;
'5': Result := ukCorporate;
'8': Result := ukSpecialRate;
'9': Result := ukPremium;
'7':
begin
if Copy(N, 1, 3) = '070' then
Result := ukPersonal
else if Copy(N, 1, 3) = '076' then
begin
{ 07624 is Isle of Man mobile, the rest of 076 is radiopaging }
if Copy(N, 1, 5) = '07624' then Result := ukStandardMobile
else Result := ukPaging;
end
else
Result := ukStandardMobile;
if (Result = ukStandardMobile) and (Length(N) <> 11) then
Result := ukInvalid;
end;
else
Result := ukInvalid;
end;
end;
function IsStandardMobile(const AText: string): Boolean;
begin
Result := Kind(AText) = ukStandardMobile;
end;
function SmsRejectReason(const AText: string): string;
begin
case Kind(AText) of
ukStandardMobile: Result := '';
ukPersonal: Result := '070 is personal numbering (forwarding), not a mobile';
ukPaging: Result := '076 is radiopaging';
ukGeographic: Result := '01 and 02 are geographic landlines';
ukNonGeographic: Result := '03 is a non-geographic landline-rate number';
ukCorporate: Result := '05 is corporate numbering, often VoIP';
ukSpecialRate: Result := '08 is freephone or special rate';
ukPremium: Result := '09 is premium rate';
else
Result := 'not a valid UK number';
end;
end;
end.
NationalForm accepts digits, spaces, dashes, dots and brackets, and one leading +. Anything else, such as a letter, returns an empty string. InternationalForm returns the E.164 form, for example +447700900123. Kind returns the block, IsStandardMobile is the yes/no version of it, and SmsRejectReason returns an empty string when the prefix looks fine or a short sentence you can show to the user.
A test program
editSave this next to uknumber.pas and compile it with fpc testuk.pas. It prints one line per check and halts with exit code 1 if anything fails.
program TestUk;
{$mode objfpc}{$H+}
uses UkNumber;
var
Failures: Integer = 0;
procedure CheckStr(const ALabel, AGot, AWant: string);
begin
if AGot <> AWant then
begin
WriteLn('FAIL ', ALabel, ': got "', AGot, '", want "', AWant, '"');
Inc(Failures);
end
else
WriteLn('ok ', ALabel);
end;
procedure CheckBool(const ALabel: string; AGot, AWant: Boolean);
begin
if AGot <> AWant then
begin
WriteLn('FAIL ', ALabel);
Inc(Failures);
end
else
WriteLn('ok ', ALabel);
end;
begin
CheckStr('national spaces', NationalForm('07700 900123'), '07700900123');
CheckStr('+44 (0) trunk', InternationalForm('+44 (0)7700 900123'), '+447700900123');
CheckStr('0044 and dashes', InternationalForm('0044 7700-900-123'), '+447700900123');
CheckStr('letters rejected', InternationalForm('07700 90012x'), '');
CheckStr('ten digit national', InternationalForm('0770090012'), '+44770090012');
CheckStr('way too short', InternationalForm('0770'), '');
CheckBool('mobile 077', IsStandardMobile('07700 900123'), True);
CheckBool('mobile 074', IsStandardMobile('+44 7400 123456'), True);
CheckBool('isle of man', IsStandardMobile('07624 123456'), True);
CheckBool('070 is personal', IsStandardMobile('070 0000 0000'), False);
CheckBool('076 is paging', IsStandardMobile('07600 000000'), False);
CheckBool('landline', IsStandardMobile('020 7946 0000'), False);
CheckBool('short 07 number', IsStandardMobile('0770 090012'), False);
CheckStr('reason freephone', SmsRejectReason('0808 157 0000'), '08 is freephone or special rate');
CheckStr('reason london', SmsRejectReason('020 7946 0000'), '01 and 02 are geographic landlines');
CheckStr('reason ok', SmsRejectReason('07700 900456'), '');
CheckStr('reason junk', SmsRejectReason('hello'), 'not a valid UK number');
if Failures = 0 then WriteLn('all passed') else Halt(1);
end.
The numbers in the tests are in the ranges Ofcom reserves for drama and examples (07700 900xxx, 020 7946 0xxx, 0808 157 0xxx), so none of them belongs to a real person.
What this does not do
editIt is a format and prefix check only. It cannot tell you whether a number is in service, whether it has been ported to another network, or whether a mobile-looking +44 7 number is really delivered over VoIP. Many services refuse virtual numbers even when the prefix looks right, so treat IsStandardMobile as necessary, not sufficient. If your input comes from a form, also keep the original text, because the national form drops the formatting the person used.
Related
editIf you need a UK number to try sign-up and SMS flows end to end, the notes on UK numbers for SMS verification from Ghost SIMs describe what a rented UK mobile number can and cannot do. I work on Ghost SIMs, so read that as a disclosure and not as a recommendation. For the exact allocation rules, the Ofcom National Telephone Numbering Plan is the source of truth.