Skip to content

Instantly share code, notes, and snippets.

@Chamuth Chamuth/lkphones.ts
Created Nov 21, 2018

Embed
What would you like to do?
Validate Sri Lankan phones numbers and get their carrier
export module LKPhoneValidator
{
export function Validate(phone : string) : PhoneValidation
{
// remove starting +94
phone = phone.replace("+94", "0");
// Consider number length
if ((phone.startsWith('0') && phone.length == 10) || (!phone.startsWith('0') && phone.length == 9) && /^\d+$/.test(phone))
{
if (phone.startsWith('0'))
phone = phone.substr(1);
var carrier = null;
var starter = phone.substr(0,2);
switch (starter)
{
case "70" :
case "71" :
carrier = CarrierType.Mobitel;
break;
case "72" :
carrier = CarrierType.Etisalat;
break ;
case "75" :
carrier = CarrierType.Airtel;
case "76" :
case "77" :
carrier = CarrierType.Dialog;
break;
case "78" :
carrier = CarrierType.Hutch;
break;
}
return new PhoneValidation(true, carrier, starter)
} else {
return new PhoneValidation(false, null, null);
}
}
export enum CarrierType
{
Airtel, Dialog, Mobitel, Etisalat, Hutch
}
export class PhoneValidation
{
Valid : boolean;
Carrier : CarrierType;
Starter : string;
constructor(valid : boolean, carrier : CarrierType, starter : string)
{
this.Valid = valid;
this.Carrier = carrier;
this.Starter = starter;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.