简体   繁体   English

使用javascript验证输入的顶级域

[英]Validate top level domain from input with javascript

How can I validate a domain name from a url input? 如何从URL输入中验证域名? I'm currently validating to www.domain.com , but need to be able to validate to just domain.com . 我目前正在对www.domain.com验证,但需要能够仅对domain.com进行验证。 So if a user enters www or http://www. 因此,如果用户输入wwwhttp://www. , it needs to show the error. ,它需要显示错误。 Also need to disable the content section from showing, unless the validation passes, instead of showing only if the input is not blank. 除非通过验证,否则还需要禁止显示内容部分,而不是仅在输入不为空白时才显示。

Currently using: 目前正在使用:

var regExp = new RegExp("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-     
9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$", "g");

but this is validating to www.domain.com 但这正在验证www.domain.com

jsFiddle: Link jsFiddle: 链接

^(?!www\\.|http:\\/\\/www\\.)(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)+([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$

Simply add negative lookahead .See demo. 只需添加negative lookahead参阅演示。

https://regex101.com/r/tS1hW2/7 https://regex101.com/r/tS1hW2/7

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

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