Skip to content

Facebook URL validation in JavaScript | Example code

  • by

Regular expressions are flexible enough to validate a FaceBook URL in JavaScript.

Code of Facebook URL validation in JavaScript

HTML example code.


		var url1 = ''; 

		var url2 = '';  
		var url3 = '';       
		var url4 = '';      
		var url5 = '';

		var url6 = '';             
		var url7 = '';         

		function validate_fb_url(url){    

			if (/^(https?:\/\/)?((w{3}\.)?)\/.*/i.test(url))
				return 'facebook';

			return 'unknown';}

		alert('This link is ' + validate_fb_url(url2));


Facebook URL validation in JavaScript

For Twitter and Facebook

function validate_url(url)
  if (/^(https?:\/\/)?((w{3}\.)?)twitter\.com\/(#!\/)?[a-z0-9_]+$/i.test(url))
     return 'twitter';    

 if (/^(https?:\/\/)?((w{3}\.)?)\/.*/i.test(url))
     return 'facebook';

 return 'unknown';

Do comment if you have any doubts and suggestion 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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.