Skip to content

JS Regex URL validation | Example code

  • by

JS Regex URL validation is very simple, just the match method.

string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);

Complete Example code URL validation in JS

Website URL validation with regex HTML code.

<html>
<body>

	<script>
		function isValidURL(string) {
			var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
			return (res !== null)
		};

		var tc6 = "https://eyehunts.com/";
		console.log(isValidURL(tc6)); 
		
	</script>
</body>

</html>

Output:

JS Regex URL validation

Do comment if you have any doubts and suggestions on this JS URL code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *