Regular Expression for null or Domain (Pattern) validation [duplicate]

邮差的信 提交于 2019-12-13 11:15:43

问题


Possible Duplicate:
What is the best regular expression to check if a string is a valid URL?

I want to validate text box by regular expression. It should be Null or valid Domain Name Like Google.com, example.com.

Thx


回答1:


Something along the following lines will work.

function isValidURL(url)
{
  return url.match(/([a-zA-Z]+:\/\/)?(.+\.)?(\w+)(\.\w+)(:[0-9]+)?(\/.+)?/) ? true : false;
}

alert(isValidURL(document.getElementById('textBoxId').value));

It won't work for things like "example.com" as really these aren't "valid" urls in that sense. A valid url beings with http://



来源:https://stackoverflow.com/questions/6058484/regular-expression-for-null-or-domain-pattern-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!