How to Syntactically Validate a URL in Javascript

Cloudmersive
1 min readMar 27, 2020

Syntactic validation is a great way to avoid broken links and other such headaches. To make life easy, we are going to skip past setting this up ourselves, instead favoring an API that is ready to do it for us.

Let’s start the process by importing our client via the following script tag.

<script src="https://cdn.cloudmersive.com/jsclient/cloudmersive-validate-client.js"></script>

Next comes our function, seen below:

var CloudmersiveValidateApiClient = require('cloudmersive-validate-api-client');var defaultClient = CloudmersiveValidateApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveValidateApiClient.DomainApi();var request = new CloudmersiveValidateApiClient.ValidateUrlRequestSyntaxOnly(); // ValidateUrlRequestSyntaxOnly | Input URL informationvar callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.domainUrlSyntaxOnly(request, callback);

Done! Now any URL entered in will be checked for syntax by the API. This same client has many other handy functions. Everything from mailing addresses to people’s names can and will be validated!

--

--

Cloudmersive

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