How to Validate First & Last Names Individually using JavaScript

Cloudmersive
2 min readAug 15, 2022

--

There are lots of different names out there — many of which are known and recorded, and many of which are completely new or using creative spellings to make familiar sounds. This can make validating name inputs tricky — but thankfully, there are APIs for that. Our individual first & last name validation APIs will identify if input names with correct characters are valid & known or valid & unknown, and they will identify certain strings (for example, those with an abundance of non-letter characters) as invalid characters. They can help clean up the data your website/app receives from its visitors, and they’re free to use with a free Cloudmersive account (you can register one on our website). Below, I’ll demonstrate how you can structure your API calls in JavaScript; you can also use these APIs as Power Automate/Logic Apps Connectors.

Let’s start with the First Name Validation API. We can structure the API call two different ways — starting with using the XHR feature in JavaScript. To go that route, use the below code examples:

var data = JSON.stringify({
"FirstName": "<string>"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "https://api.cloudmersive.com/validate/name/first");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Apikey", "YOUR-API-KEY-HERE");
xhr.send(data);

To use jQuery instead, first run the below command to install:

bower install jquery

And then include the below code examples:

var settings = {
"url": "https://api.cloudmersive.com/validate/name/first",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json",
"Apikey": "YOUR-API-KEY-HERE"
},
"data": JSON.stringify({
"FirstName": "<string>"
}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});

All done — nice and easy.

Now let’s move on to the Last Name validation API. Let’s start with the XHR code examples once again:

var data = JSON.stringify({
"LastName": "<string>"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "https://api.cloudmersive.com/validate/name/last");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Apikey", "YOUR-API-KEY-HERE");
xhr.send(data);

For jQuery, we’re going to use the same command as before to install the library:

bower install jquery

And then use the below code snippet:

var settings = {
"url": "https://api.cloudmersive.com/validate/name/last",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json",
"Apikey": "YOUR-API-KEY-HERE"
},
"data": JSON.stringify({
"LastName": "<string>"
}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});

No more code required. Put your name validation needs to rest!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet