javascript jquery如何验证域名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7916370/
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
Jquery how to validate domain name
提问by Rails beginner
I have a input field where users can write a domain name that they wish for search for.
我有一个输入字段,用户可以在其中输入他们希望搜索的域名。
My domain search only support searches as: "something.com" or "something"
我的域搜索仅支持搜索为:“something.com”或“something”
And it only support .com, .net, .org
它只支持 .com, .net, .org
That I want to do is validate the user input before the ajax call is made and display an error message if anything is wrong. Example if a user types: "asdasdasd.asdasd" or "wwwasd.asdasd" or "www.asdasd.dk"
我想要做的是在进行 ajax 调用之前验证用户输入,并在出现任何错误时显示错误消息。例如,如果用户键入:“asdasdasd.asdasd”或“wwwasd.asdasd”或“www.asdasd.dk”
#domainsearch is the input field
#domanerknap is the search button
Here is my Jquery:
这是我的 Jquery:
$(document).ready(function() {
$('#domanerknap').click(function(e) {
var doma = $('#domainsearch').val();
$('#contentajax').empty()
.css("border", "1px solid #CCCCCC")
.html('<p class="vent">Please wait...</p><p class="venter"><img src="../images/ajax.gif" /></p>');
$.ajax({
type: 'POST',
url: 'http://localhost:3000/domain',
data: {
domain: doma
},
success: function(msg){
$('#contentajax').html(msg);
$('#contentajax').css("border", "none");
}
});
});
});
采纳答案by gion_13
use a regex to check if the domain is valid. Such a regex might be :
使用正则表达式检查域是否有效。这样的正则表达式可能是:
/^(http(s)?\/\/:)?(www\.)?[a-zA-Z\-]{3,}(\.(com|net|org))?$/
Then before you send the ajax call, check to see if the input matches the regex :
然后在发送 ajax 调用之前,检查输入是否与正则表达式匹配:
$(document).ready(function() {
$('#domanerknap').click(function(e) {
var doma = $('#domainsearch').val();
if(!/^(http(s)?\/\/:)?(www\.)?[a-zA-Z\-]{3,}(\.(com|net|org))?$/.test(doma))
{
alert('invalid domain name');
return false;
}
$('#contentajax').empty()
.css("border", "1px solid #CCCCCC")
.html('<p class="vent">Please wait...</p><p class="venter"><img src="../images/ajax.gif" /></p>');
$.ajax({
type: 'POST',
url: 'http://localhost:3000/domain',
data: {
domain: doma
},
success: function(msg){
$('#contentajax').html(msg);
$('#contentajax').css("border", "none");
}
});
});
});
回答by Rob W
Use the pattern as shown below. To construct this pattern, I used the following rules:
使用如下所示的模式。为了构建这个模式,我使用了以下规则:
- The domain must not contain a protocol
- Only alphanumeric characters and hypens are a valid domain name
- Sub domains are allowed
- Top level domain must be either of the following:
com net org
- 域不得包含协议
- 只有字母数字字符和连字符是有效的域名
- 允许子域
- 顶级域必须是以下之一:
com net org
I have also included a dynamic match, so that protocols and paths are automatically omitted when input.
我还包含了一个动态匹配,以便在输入时自动省略协议和路径。
var domainPattern = /^(https?:\/\/)?((?:[a-z0-9-]+\.)+(?:com|net|org))(?:\/|$)/i
if(doma.match(domainPattern)){
doma = doma[1]; //Get the domain parth
$.ajax(...); //Valid
}
回答by Niet the Dark Absol
if( !doma.match(/^[a-z0-9_-]+(\.(com|net|org))?$/i)) { /* error stuff here */
else $.ajax(...);
Something like that, I would imagine, going by the examples of valid input you gave.
像这样的事情,我想,通过你提供的有效输入的例子。
You can also put the part between the /.../
in the pattern
attribute of the input
element, and supporting browsers will match it before even allowing a submission.
您也可以将部分放在元素的/.../
inpattern
属性之间input
,支持的浏览器甚至会在允许提交之前匹配它。