TestPhoneNumber.psm1
function Test-PhoneNumber { param( [string]$PhoneNumber ) $ValidNAAreaCodes = '201', '202', '203', '204', '205', '206', '207', '208', '209', '210', '212', '213', '214', '215', '216', '217', '218', '219', '220', '223', '224', '225', '226', '227', '228', '229', '231', '234', '236', '239', '240', '242', '246', '248', '249', '250', '251', '252', '253', '254', '256', '260', '262', '264', '267', '268', '269', '270', '272', '274', '276', '279', '281', '283', '284', '289', '301', '302', '303', '304', '305', '306', '307', '308', '309', '310', '312', '313', '314', '315', '316', '317', '318', '319', '320', '321', '323', '325', '327', '330', '331', '332', '334', '336', '337', '339', '340', '343', '345', '346', '347', '351', '352', '360', '361', '364', '365', '367', '380', '385', '386', '401', '402', '403', '404', '405', '406', '407', '408', '409', '410', '412', '413', '414', '415', '416', '417', '418', '419', '423', '424', '425', '430', '431', '432', '434', '435', '437', '438', '440', '441', '442', '443', '445', '447', '450', '456', '458', '463', '464', '469', '470', '473', '475', '478', '479', '480', '484', '500', '501', '502', '503', '504', '505', '506', '507', '508', '509', '510', '512', '513', '514', '515', '516', '517', '518', '519', '520', '522', '530', '531', '533', '534', '539', '540', '541', '544', '548', '551', '557', '559', '561', '562', '563', '564', '566', '567', '570', '571', '573', '574', '575', '577', '579', '580', '581', '585', '586', '587', '588', '600', '601', '602', '603', '604', '605', '606', '607', '608', '609', '610', '612', '613', '614', '615', '616', '617', '618', '619', '620', '622', '623', '626', '628', '629', '630', '631', '636', '639', '641', '646', '647', '649', '650', '651', '657', '659', '660', '661', '662', '664', '667', '669', '670', '671', '678', '679', '680', '681', '682', '684', '689', '700', '701', '702', '703', '704', '705', '706', '707', '708', '709', '710', '712', '713', '714', '715', '716', '717', '718', '719', '720', '721', '724', '725', '726', '727', '730', '731', '732', '734', '737', '740', '743', '747', '754', '757', '758', '760', '762', '763', '765', '767', '769', '770', '772', '773', '774', '775', '778', '779', '780', '781', '782', '784', '785', '786', '787', '800', '801', '802', '803', '804', '805', '806', '807', '808', '809', '810', '812', '813', '814', '815', '816', '817', '818', '819', '822', '825', '828', '829', '830', '831', '832', '833', '838', '843', '844', '845', '847', '848', '849', '850', '854', '855', '856', '857', '858', '859', '860', '862', '863', '864', '865', '866', '867', '868', '869', '870', '872', '873', '876', '877', '878', '879', '888', '900', '901', '902', '903', '904', '905', '906', '907', '908', '909', '910', '912', '913', '914', '915', '916', '917', '918', '919', '920', '925', '928', '929', '930', '931', '934', '936', '937', '938', '939', '940', '941', '947', '949', '951', '952', '954', '956', '959', '970', '971', '972', '973', '975', '978', '979', '980', '984', '985', '986', '989' $PhoneNumber = $PhoneNumber -Replace '\D', '' if ($PhoneNumber.Length -eq 11) { if ($PhoneNumber.Substring(0, 1) -ne 1) { return $false } $PhoneNumber = $PhoneNumber.Substring(1) } if ($PhoneNumber.Length -ne 10) {return $false} $areaCode = $PhoneNumber.Substring(0, 3) $exchangeCode = $PhoneNumber.Substring(3, 3) $stationCode = $PhoneNumber.Substring(6) if ($areaCode -notin $ValidNAAreaCodes) { return $false } if ($exchangeCode[1] -eq '1' -and $exchangeCode[2] -eq '1') { return $false } if ($exchangeCode -eq '555') { if ($stationCode -eq '1212' -Or $stationCode -eq '4334') { return $false } if ($stationCode[0] -eq '0') { [int]$stationCodeNumber = $stationCode if ($stationCodeNumber -ge 100 -and $stationCodeNumber -le 199) { return $false } } } $true } |