How to Validate a Phone Number using Java

2 min readSep 21, 2022


No matter what business you’re in, phone numbers are important to get right. They represent a means of directly communicating with our clients and customers, so invalid phone number entries have a direct negative impact on your business. Thankfully, you can easily ensure high-quality data entry with our Phone Number Validation API. This API will determine whether a given phone number (entered as a string) is valid based on a country specification of your choosing, ensuring incorrect/invalid data won’t make it to your database.

The best part? You can use this API for free by following instructions below to install the API client & structure your API call with Java code examples. Beyond that, you’ll just need to get an API key by registering a free account on our website (free accounts come with a limit of 800 API calls per month; perfect for small-scale projects and startups).

We can begin installing the client by first adding a reference to the repository in pom.xml:


And we can wrap up installation by adding one more reference to the pom.xml dependency:


Next, we can turn our attention to the controller and include our imports at the top of our file:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.PhoneNumberApi;

Lastly, we can call the function using the code examples below. Before we do that, let’s first structure our request in the following JSON format:

"PhoneNumber": "string",
"DefaultCountryCode": "string"

And now let’s call the function & include our API key:

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
PhoneNumberApi apiInstance = new PhoneNumberApi();
PhoneNumberValidateRequest value = new PhoneNumberValidateRequest(); // PhoneNumberValidateRequest | Phone number to validate in a PhoneNumberValidateRequest object. Try a phone number such as \"1.800.463.3339\", and either leave DefaultCountryCode blank or use \"US\".
try {
PhoneNumberValidationResponse result = apiInstance.phoneNumberSyntaxOnly(value);
} catch (ApiException e) {
System.err.println("Exception when calling PhoneNumberApi#phoneNumberSyntaxOnly");

And that’s it — no more code required!




There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.