简体   繁体   English

通过javascript验证网站/域

[英]Validate site/domain via javascript

I have a standard html form where the user can specify a website/domain: 我有一个标准的html表单,用户可以在其中指定一个网站/域:

favourite website: [ ] 最喜欢的网站:[]

I expect the user to type things like: 我希望用户键入以下内容:

[www.google.com] [www.yahoo.com] [www.example.co.jp] [www.google.com] [www.yahoo.com] [www.example.co.jp]

I want to validate the domain/site via javascript so if a user had typed something wrong: 我想通过javascript验证域/站点,因此如果用户输入了错误的内容:

[www...google.com] [www.-example.com] [www ... google.com] [www.-example.com]

I tell them straight away. 我马上告诉他们。

Can anybody post this javascript ? 有人可以发布此javascript吗?

Alternatively can anybody point me to a site where I can look at their javascript ? 或者,有人可以将我指向一个可以查看其javascript的网站吗?

Many Thanks 非常感谢

Doing a Google search I found this: Validating Domain Names in a blog by Shaun Inman. 在Google搜索中,我发现了这一点:Shaun Inman在博客中验证域名

His method appears robust and may be exactly what you're looking for. 他的方法看起来很健壮,可能正是您要寻找的方法。 I would be a bit concerned about having to stay on top of any changes to the TLD list. 我会担心必须紧跟TLD列表的任何更改。

Another option, as one reader on his blog suggested, is to keep it simple and check for one character, a dot, and at least two characters after the dot. 正如其博客上的一位读者所建议的那样,另一种选择是保持简单,并检查一个字符,一个点以及在该点后至少两个字符。 Ex: a.bc . 例如: a.bc That has the advantage of not requiring maintenance and not frustrating your users. 这样做的好处是不需要维护并且不会使用户感到沮丧。

Another option, if you want to be very robust is to perform a DNS lookup. 如果要非常强大,另一种选择是执行DNS查找。 You won't be able to do that in your JavaScript alone, but with a simple Ajax call you can get it to work server side. 您将无法仅使用JavaScript来完成此操作,但是通过一个简单的Ajax调用,您就可以将其用于服务器端。

JavaScript: (uses jQuery ) JavaScript :(使用jQuery

// domain_msg is the id of the div you want your answer placed
var domain_to_lookup = 'www.google.com';
jQuery('#domain_msg').load('http://yourwebsite.com/ajaxurl', {lookup: domain_to_lookup});

Of course you'll need a server side script to check the domain. 当然,您将需要服务器端脚本来检查域。 $_POST['lookup'] will be the value (www.google.com in this example) and you can pass that to gethostbyname() (PHP). $ _POST ['lookup']将是值(在本示例中为www.google.com),您可以将其传递给gethostbyname() (PHP)。 I'm sure other languages have a similar mechanism. 我确定其他语言也有类似的机制。 Just return "Domain OK" or "Enter a valid Domain" or whatever your want your text to be. 只需返回“域名确定”或“输入有效域”或任何您想要的文本即可。 If you're new to Ajax or jQuery, this post will help you out: Using AJAX to Validate Forms . 如果您不熟悉Ajax或jQuery,这篇文章将为您提供帮助: 使用AJAX验证表单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM