javascript 使用 jquery 验证 Facebook 和 Twitter URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9171301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validating Facebook and Twitter URL using jquery
提问by Mas User
In my application I am using jquery validation for forms.
在我的应用程序中,我对表单使用 jquery 验证。
There are two other fileds to enter the twitter page url and facebook page url.
还有两个其他字段可以输入 twitter 页面 url 和 facebook 页面 url。
How can I validate these url using jquery?
如何使用 jquery 验证这些 url?
Examples:
例子:
http://twitter.com/anypage
http://twitter.com/#!/anypage
http://facebook.com/anypage
回答by Panos Kal.
None of the above solutions/regular expressions are flexible enough.
Check my codein jsFiddle.
上述解决方案/正则表达式都不够灵活。
检查我在 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));?
回答by Eduardo Iglesias
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");
}
}
Source: http://www.webdeveloper.com/forum/showthread.php?t=247621
来源:http: //www.webdeveloper.com/forum/showthread.php?t= 247621
回答by Cheery
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/
或者一个验证函数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));
回答by uday
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).
使用插件中的url()
方法jQuery Validation
。它检查输入的 url 是否有效。您仍然可以根据您的要求对其进行自定义(即检查该页面是否属于 twitter 或 FB)。