Validate US area codes are valid or not in phone number field

Hello All,
I have a phone number field in my application. After entering the US area code + mobile number , We have to validate whether the area code + number in US are valid or not . Looks like there are more than 50 area code . Reference - State Area Codes – 50states . Can someone suggest how to achieve this in robot framework .

Hi @smitha,

You didn’t mention if you needed to determine the state for the area code if it is a US area code? In the example below I logged the state, but if you don’t need it &{USAreaCodes} reference variable could be a simple list instead of a dictionary.

You’ll need to finish constructing the &{USAreaCodes} dictionary of area codes, but something like the example below should do what you want.


*** Settings ***

# Library		FakerLibrary
Library		String

Test Template	Check Phone Number is US State

*** Variables ***

&{USAreaCodes}	205=Alabama 	251=Alabama 	256=Alabama 	334=Alabama 	659=Alabama 	938=Alabama
...			 	907=Alaska
...			 	480=Arizona 	520=Arizona 	602=Arizona 	623=Arizona 	928=Arizona
...			 	479=Arkansas 	501=Arkansas 	870=Arkansas
...			 	209=California 	213=California 	279=California 	310=California 	323=California 	341=California
...			 	408=California 	415=California 	424=California 	442=California 	510=California 	530=California
...			 	559=California 	562=California 	619=California 	626=California 	628=California 	650=California
...			 	657=California 	661=California 	669=California 	707=California 	714=California 	747=California
...			 	760=California 	805=California 	818=California 	820=California 	831=California 	858=California
...			 	909=California 	916=California 	925=California 	949=California 	951=California
...			 	303=Colorado 	719=Colorado 	720=Colorado 	970=Colorado
...			 	203=Connecticut 	475=Connecticut 	860=Connecticut 	959=Connecticut
...			 	302=Delaware
...			 	239=Florida 	305=Florida 	321=Florida 	352=Florida 	386=Florida 	407=Florida
...			 	561=Florida 	689=Florida 	727=Florida 	754=Florida 	772=Florida 	786=Florida
...			 	813=Florida 	850=Florida 	863=Florida 	904=Florida 	941=Florida 	954=Florida
...			 	229=Georgia 	404=Georgia 	470=Georgia 	478=Georgia 	678=Georgia 	706=Georgia
...			 	762=Georgia 	770=Georgia 	912=Georgia
...			 	808=Hawaii
...			 	208=Idaho 	986=Idaho
...			 	217=Illinois 	224=Illinois 	309=Illinois 	312=Illinois 	331=Illinois 	618=Illinois
...			 	630=Illinois 	708=Illinois 	773=Illinois 	779=Illinois 	815=Illinois 	847=Illinois
...			 	872=Illinois
...			 	219=Indiana 	260=Indiana 	317=Indiana 	463=Indiana 	574=Indiana 	765=Indiana
...			 	812=Indiana 	930=Indiana

*** Test Cases ***
+1-808-905-4708x90945	+1-808-905-4708x90945
(872)561-0149			(872)561-0149
+1-938-784-7239x816		+1-938-784-7239x816
618.337.4457x2287		618.337.4457x2287
973.664.1409			973.664.1409
001-004-525-2348		001-004-525-2348
(940)447-1822x831		(940)447-1822x831
001-251-378-4770x0565	001-251-378-4770x0565

*** Keywords ***
Check Phone Number is US State
	[Arguments]		${number}
	# reformat number to consistant format
	${number}= 	Replace String 	${number} 	x	-
	${number}= 	Replace String 	${number} 	.	-
	${number}= 	Replace String 	${number} 	)	-
	${number}= 	Replace String 	${number} 	(	${EMPTY}

	# split the number to of parts
	${numparts}=	Split String	${number}	-

	${area}=	Set Variable    ${numparts[0]}

	# check if first part os country code? if so check for non USA code and use second part as state code if USA country code
	IF 	"${area[0]}" == "+"
		${ccode}= 	Replace String 	${area} 	+	${EMPTY}
		IF	int("${ccode}") > 1
			Fail    Non USA Number (country code > 1)
		${area}=	Set Variable    ${numparts[1]}
	IF 	"${area[0]}" == "0"
		IF 	${area[0]} != "001"
			Fail    Non USA Number (country code > 1)
		${area}=	Set Variable    ${numparts[1]}

	# check the area code against known good USA area codes
	IF 	'${area}' in &{USAreaCodes}
		Pass Execution 		${area} is ${USAreaCodes['${area}']}
		Fail    ${area} is not in ${USAreaCodes}

1 Like

Thank you so much for your quick response . I have few clarifications . In the below image, when we select US it has to accept the (3 digit US area code)+(7 digit number) and if its not an US area code it will display an error.


I’m not sure what your question is?

you could cycle through area code numbers sequentially from 000 to 999, use the keyword I gave you or an adapted version to check if the area code is valid, then based on that when you enter the number you can test for the presence of the error message you expect if it not a valid area code and test for the non-presence of the error message if it is a valid area code. if you are not testing the numbers them selves you can probably just use 111-1111 as the number to keep things simple.