How to Validate First and Last Names using Java

Valid data entry is critically important for database SEO. If customers are entering their names into website fields (while creating an account, for example), you should ensure that their first & last names only pass through our system if they’re typed correctly with valid characters, or you’ll end up with a lot of messy and/or useless data. Thankfully, you easily validate names using our First & Last Name Validation APIs. Both of these APIs will provide a ValidationResult with possible values including ValidFirstName, ValidUnknownFirstName, InvalidSpamInput, InvalidCharacters, and InvalidEmpty.

To provide further context for these responses: there are two valid result options to accommodate for the fact that phonetically similar names are often spelled in unique ways. To provide an example, think of the following known spellings of a single name: Conor, Connor & Conner. Given the multitude of unique spellings, typing “Connar” instead of those options would be considered a ValidUnknownFirstName. The invalid result options are only accommodating for spam, invalid characters (numbers & symbols), or missing data.

Below, I’ll demonstrate how to install the API client and use ready-to-run Java code examples to structure your API call. Other than that, you’ll just need a Cloudmersive API key to authenticate the service, which you can get for free by registering a free account on our website (free accounts come with 800 API calls per month & zero additional commitments: perfect for small-scale projects/startups).

Let’s start installation by adding a reference to the pom.xml repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

And then let’s add one to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

With installation complete, let’s add the imports:

// 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.NameApi;

Before we include code to call either of these APIs, let’s first structure our validation request information properly. Each input follows a similar JSON format:

FIRST NAME VALIDATION REQUEST FORMAT{
"FirstName": "string"
}
LAST NAME VALIDATION REQUEST FORMAT{
"LastName": "string"
}

Now let’s include the code to call the First Name validation API. Copy the below, and pass through your argument in the format provided above. Then include your 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)
//Apikey.setApiKeyPrefix("Token");
NameApi apiInstance = new NameApi();
FirstNameValidationRequest input = new FirstNameValidationRequest(); // FirstNameValidationRequest | Validation request information
try {
FirstNameValidationResponse result = apiInstance.nameValidateFirstName(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling NameApi#nameValidateFirstName");
e.printStackTrace();
}

Lastly, let’s include the code to call the Last Name validation API. Use the below code:

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)
//Apikey.setApiKeyPrefix("Token");
NameApi apiInstance = new NameApi();
LastNameValidationRequest input = new LastNameValidationRequest(); // LastNameValidationRequest | Validation request information
try {
LastNameValidationResponse result = apiInstance.nameValidateLastName(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling NameApi#nameValidateLastName");
e.printStackTrace();
}

After that, you’re all done — no more code required. No need to worry about sloppy name entry anymore.

--

--

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.