简体   繁体   中英

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

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(form["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:

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:

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]))*\\.)+[az]+(\\/[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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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