Check if string is a valid url with Javascript, Typescript and Php

It is fairly common for web and remote applications to operate with several URLs, and most of the time we may need to verify that a provided string is correctly defined as an http URL just before trying to access it. While this verification implies a fairly complex string comparison code that we can perform by ourselves with a regular expression, we can use the TurboCommons library StringUtils class to get a reliable and fast result.

You can play and test it online here

Test that string is a valid URL with the TurboCommons library

There's a general purpose isUrl() method inside the StringUtils class which lets you pass any value (string or not) to check if it represents a correct internet URL. Let's see how to do it with for different programming languages:

Test if string is an URL with Php

Download the latest TurboCommons phar file from the downloads section, place it on your project as a dependency and run the following code:

require '%path-to-your-project-dependencies-folder%/turbocommons-php-X.X.X.phar';
use org\turbocommons\src\main\php\utils\StringUtils;
echo StringUtils::isUrl('dfgsdgsdgsdfg')."<br>";
echo StringUtils::isUrl('google.com')."<br>";
echo StringUtils::isUrl('http://google.com')."<br>";
echo StringUtils::isUrl('https://google.com')."<br>";

The output of the previous code should be:

false
false
true
true

Test if string is an URL with Javascript on a website

Download the latest turbocommons-es5.js file from the downloads section or use npm to add the dependecy to your project (npm install turbocommons-es5). Then run the following code:

<script src="turbocommons-es5/turbocommons-es5.js"></script>
<script>
var StringUtils = org_turbocommons.StringUtils;
console.log(StringUtils.isUrl('dfgsdgsdgsdfg'));
console.log(echo StringUtils.isUrl('google.com'));
console.log(echo StringUtils.isUrl('http://google.com'));
console.log(echo StringUtils.isUrl('https://google.com'));
</script>

The console log for the previous code should be:

false
false
true
true

Test if string is an URL with Typescript (TS)

The recommended way is to use npm to obtain the turbocommons dependency by executing the following command at the root of your project:

npm install turbocommons-ts

Or you can download the latest turbocommons-ts files from the downloads section and copy the dependency by yourself. Then run the following code:

import { StringUtils } from 'turbocommons-ts';
console.log(StringUtils.isUrl('dfgsdgsdgsdfg'));
console.log(echo StringUtils.isUrl('google.com'));
console.log(echo StringUtils.isUrl('http://google.com'));
console.log(echo StringUtils.isUrl('https://google.com'));

The output of the previous code should be:

false
false
true
true

Test if string is an URL with a NodeJs App

Install the dependency by executing the following command at the root of your project:

npm install turbocommons-ts

And then run the following code:

const {StringUtils} = require('turbocommons-ts');
console.log(StringUtils.isUrl('dfgsdgsdgsdfg'));
console.log(echo StringUtils.isUrl('google.com'));
console.log(echo StringUtils.isUrl('http://google.com'));
console.log(echo StringUtils.isUrl('https://google.com'));

Which should output:

false
false
true
true