Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating Facebook and Twitter URL using jquery

In my application I am using jquery validation for forms.

There are two other fileds to enter the twitter page url and facebook page url.

How can I validate these url using jquery?

Examples:

http://twitter.com/anypage

http://twitter.com/#!/anypage

http://facebook.com/anypage
like image 626
Mas User Avatar asked Feb 07 '12 05:02

Mas User


3 Answers

None of the above solutions/regular expressions are flexible enough.
Check my 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));​
like image 165
Panos Kal. Avatar answered Nov 07 '22 01:11

Panos Kal.


I think this may 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");
      }
  }

For Twitter just add new var for Twitter URL

var TWurl = /^(http|https)\:\/\/(www.|)twitter.com\/.*/i;

Validate:

  1. http://twitter.com/username > TRUE
  2. https://twitter.com/username > TRUE
  3. http://www.twitter.com/username > TRUE
  4. https://www.twitter.com/username > TRUE

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

like image 25
Eduardo Iglesias Avatar answered Nov 07 '22 00:11

Eduardo Iglesias


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 a validation 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));
like image 40
Cheery Avatar answered Nov 07 '22 01:11

Cheery