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
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));
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:
Source: http://www.webdeveloper.com/forum/showthread.php?t=247621
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With