Checking Facebook and Twitter URLs with jquery

In my application, I use jquery validation for forms.

There are two other files for entering the url and facebook page URLs.

How can I check this url using jquery?

Examples:

http://twitter.com/anypage http://twitter.com/#!/anypage http://facebook.com/anypage 
+4
source share
4 answers

None of the above solutions / regular expressions are flexible enough.
Check the code in jsFiddle.


 var str1 = 'http://twitter.com/anypage'; //True var str2 = 'http://twitter.com/#!/anypage'; //True var str3 = 'http://facebook2.com/anypage'; //False var str4 = 'http://www.facebook.com/anypage'; //True http & www var str5 = 'http://facebook.com/anypage'; //True http var str6 = 'https://facebook.com/anypage'; //True https var str7 = 'https://www.facebook.com/anypage'; //True https & www var str8 = 'facebook.com/anypage'; //True no protocol var str9 = 'www.facebook.com/anypage'; //True no protocol & www function validate_url(url) { if (/^(https?:\/\/)?((w{3}\.)?)twitter\.com\/(#!\/)?[a-z0-9_]+$/i.test(url)) return 'twitter'; if (/^(https?:\/\/)?((w{3}\.)?)facebook.com\/.*/i.test(url)) return 'facebook'; return 'unknown'; } alert('This link is ' + validate_url(str2));​ 
+7
source

I think it can help you.

 function validFBurl(enteredURL) { var FBurl = /^(http|https)\:\/\/www.facebook.com\/.*/i; if(!enteredURL.match(FBurl)) { alert("This is not a Facebook URL"); } else { alert("This IS a Facebook URL"); } } 

Source: http://www.webdeveloper.com/forum/showthread.php?t=247621

+1
source

Like this?

 var str1 = 'http://twitter.com/anypage'; var str2 = 'http://twitter.com/#!/anypage'; var str3 = 'http://facebook.com/anypage'; if (/https?:\/\/twitter\.com\/(#!\/)?[a-z0-9_]+$/i.test(str1)) alert('Str1 has passed first regexp'); if (/https?:\/\/twitter\.com\/(#!\/)?[a-z0-9_]+$/i.test(str2)) alert('Str2 has passed first regexp'); if (/https?:\/\/facebook\.com\/[a-z0-9_]+$/i.test(str3)) alert('Str3 has passed second regexp'); 

Or check function http://jsfiddle.net/36Wct/2/

 var str1 = 'http://twitter.com/anypage'; var str2 = 'http://twitter.com/#!/anypage'; var str3 = 'http://facebook.com/anypage'; var str4 = 'http://facebook2.com/anypage'; function validate_url(url) { if (/https?:\/\/twitter\.com\/(#!\/)?[a-z0-9_]+$/i.test(url)) return 'twitter'; if (/https?:\/\/facebook\.com\/[a-z0-9_]+$/i.test(url)) return 'facebook'; return 'unknown'; } alert('This link is ' + validate_url(str4)); 
+1
source

Use the url() method from the jQuery Validation plugin. It checks if the URL entered is valid. You can customize it as you wish (to check if the page belongs to Twitter or FB).

Source: http://docs.jquery.com/Plugins/Validation

0
source

Source: https://habr.com/ru/post/1395153/


All Articles