开发者

validate URL in form http://www.test.com

开发者 https://www.devze.com 2023-01-12 21:40 出处:网络
I want to validate URL (using java script) to accept only format - http://www.test.com. I\'m trying following code, but it accepts test.com and http://www.test.com.com also.

I want to validate URL (using java script) to accept only format - http://www.test.com. I'm trying following code, but it accepts test.com and http://www.test.com.com also.

var URLReg = new RegExp();
URLReg.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
if (!URLReg.test(f开发者_运维知识库orm["URL"].value)) {
    alert("You must supply a valid URL.");
    return false;
} 

what is wrong in this ? :(

Thanks in advance.


If you want to stick partially to your code a quick fix could simply use these:

"^((https?\\://www\\.)|(www\\.))[A-Za-z0-9_\\-]+\\.[A-Za-z0-9_\\-\\%\\&\\?\\.\\=\\+]+$" (accepts "http://www...." and "www...")

or

"^(https?\\://www\\.)[A-Za-z0-9_\\-]+\\.[A-Za-z0-9_\\-\\%\\&\\?\\.\\=\\+]+$" (accepts only "http://www...." and NOT "www...")

Neither ones of the above accepts a domain without "www".

Anyway your code (and also the adjusted code I placed above) is WRONG because they would both validate ok domains like these:

  • http://www.dom_ain.com
  • http://www.-domain.com
  • http://www.subdomain.domain..gee%%%==all-crap-still-ok

Your regex would also accept a crap like this:

  • cheers://www...

To validate just the domain part (forcing www to be entered) you can use this:

var URLReg = /^((https?\://wwww\.)|(www\.))([a-z0-9]([a-z0-9]|(\-[a-z0-9]))*\.)+[a-z]+$/i;

This one won't validate ok crap like:

  • http://www.dom_ain.com
  • http://www.-domain.com

BTW: It would validate also http://www.domain.com.com but this is not an error because a subdomain url could be like: http://www.subdomain.domain.com and it's valid! And there is almost no way (or at least no operatively easy way) to validate for proper domain tld with a regex because you would have to write inline into your regex all possible domain tlds ONE BY ONE like this:

var URLReg = /^((https?\://wwww\.)|(www\.))([a-z0-9]([a-z0-9]|(\-[a-z0-9]))*\.)+(com|it|net|uk|de)$/i;

(this last one for instance would validate only domain ending with .com/.net/.de/.it/.co.uk) New tlds always come out, so you would have to adjust you regex everytimne a new tld comes out, that's odd!


In order to validate also the remaining part of an url you could add the remainig part at the end of the regex:

var URLReg = /^((https?\://wwww\.)|(www\.))([a-z0-9]([a-z0-9]|(\-[a-z0-9]))*\.)+[a-z]+(\/[a-z0-9_\-\%\&\?\.\=\+]*)*$/i;

It's still not perfect because somone could enter:

http://www.domain.com/????hello

and it would validate ok, but now I'm tired, sorry! :)


i use this expression - its the best i found:

^(https?://)?(([\w!~*'().&=+$%-]+: )?[\w!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([\w!~*'()-]+\.)*
([\w^-][\w-]{0,61})?[\w]\.[a-z]{2,6})(:[0-9]{1,4})?((/*)|(/+[\w!~*'().;?:
@&=+$,%#-]+)+/*)$


See if

^[A-Za-z]+:\/\/[A-Za-z0-9-/]+\.com$

is what you want

0

精彩评论

暂无评论...
验证码 换一张
取 消