How to Validate a Phone Number using Java

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:

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:

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

// 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)
//Apikey.setApiKeyPrefix("Token");
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);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling PhoneNumberApi#phoneNumberSyntaxOnly");
e.printStackTrace();
}

And that’s it — no more code required!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

Cloudmersive

436 Followers

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