Skip to content Skip to sidebar Skip to footer

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 URL

Solution 1:

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:

varURLReg = /^((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:

varURLReg = /^((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! :)

Solution 2:

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!~*'().;?:
@&=+$,%#-]+)+/*)$

Solution 3:

See if

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

is what you want

Post a Comment for "Validate Url In Form Http://www.test.com"