Created
November 21, 2018 04:12
-
-
Save Chamuth/836fd8958d87f3c7a9aec34d0aa828fd to your computer and use it in GitHub Desktop.
Validate Sri Lankan phones numbers and get their carrier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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