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:
!/anypage
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
Share
Improve this question
edited Feb 7, 2012 at 5:35
user529758
asked Feb 7, 2012 at 5:11
Mas UserMas User
1662 gold badges5 silver badges17 bronze badges
3
- What criteria you want to validate? Like: Is it valid url, required field or whatever – uday Commented Feb 7, 2012 at 5:15
- Why don't you just ask for a username instead of a URL? – SLaks Commented Feb 7, 2012 at 5:17
- not required validation i meaned url validation and the url must be the twitter page and facebook page – Mas User Commented Feb 7, 2012 at 5:17
4 Answers
Reset to default 13None 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:
- http://twitter.com/username > TRUE
- https://twitter.com/username > TRUE
- http://www.twitter.com/username > TRUE
- https://www.twitter.com/username > TRUE
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));
Use url()
method from the jQuery Validation
plugin. It checks if the entered url is valid. You can still customize it upon your requirement (which is to check if the page belongs to twitter or FB).
Source: http://docs.jquery.com/Plugins/Validation