How to validate a Phone Number in Node.JS

In this lesson, I’m going to demonstrate for you a method for validating phone numbers, and it’s going to be a piece of cake. There are many reasons to filter out fake phone numbers — saving time for your customer service reps and weeding out fake form submissions, to name a couple.

To begin with, we need to install our API client.

npm install cloudmersive-validate-api-client --save

With access now available to our API functions, we can call this one for phone number validation:

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.PhoneNumberApi();var value = new CloudmersiveValidateApiClient.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\".var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.phoneNumberSyntaxOnly(value, callback);

And there you have it. Your phone number validation problems are solved! This same API offers a ton of similar functions, including email and mailing address validation, which can be great time savers for any business.

Image for post
Image for post

Written by

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

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