Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to stick partially to your code a quick fix could simply use these:</p> <p><code>"^((https?\\://www\\.)|(www\\.))[A-Za-z0-9_\\-]+\\.[A-Za-z0-9_\\-\\%\\&amp;\\?\\.\\=\\+]+$"</code> (accepts "<a href="http://www">http://www</a>...." and "www...")</p> <p>or</p> <p><code>"^(https?\\://www\\.)[A-Za-z0-9_\\-]+\\.[A-Za-z0-9_\\-\\%\\&amp;\\?\\.\\=\\+]+$"</code> (accepts only "<a href="http://www">http://www</a>...." and NOT "www...")</p> <p><strong>Neither ones of the above accepts a domain without "www".</strong></p> <p>Anyway your code (and also the adjusted code I placed above) is WRONG because they would both validate ok domains like these:</p> <ul> <li><a href="http://www.dom_ain.com" rel="nofollow noreferrer">http://www.dom_ain.com</a></li> <li><a href="http://www.-domain.com" rel="nofollow noreferrer">http://www.-domain.com</a></li> <li><a href="http://www.subdomain.domain..gee%%%==all-crap-still-ok" rel="nofollow noreferrer">http://www.subdomain.domain..gee%%%==all-crap-still-ok</a></li> </ul> <p>Your regex would also accept a crap like this:</p> <ul> <li>cheers://www...</li> </ul> <p>To validate <strong>just the domain part</strong> (<em>forcing www to be entered</em>) you can use this:</p> <pre><code>var URLReg = /^((https?\://wwww\.)|(www\.))([a-z0-9]([a-z0-9]|(\-[a-z0-9]))*\.)+[a-z]+$/i; </code></pre> <p>This one won't validate ok crap like:</p> <ul> <li><a href="http://www.dom_ain.com" rel="nofollow noreferrer">http://www.dom_ain.com</a></li> <li><a href="http://www.-domain.com" rel="nofollow noreferrer">http://www.-domain.com</a></li> </ul> <p>BTW: It would validate also <a href="http://www.domain.com.com" rel="nofollow noreferrer">http://www.domain.com.com</a> but this is not an error because a subdomain url could be like: <a href="http://www.subdomain.domain.com" rel="nofollow noreferrer">http://www.subdomain.domain.com</a> and it's valid! And there is almost no way (or at least no operatively easy way) to <strong>validate for proper domain tld</strong> with a regex because you would have to <strong>write inline into your regex all possible domain tlds ONE BY ONE</strong> like this:</p> <pre><code>var URLReg = /^((https?\://wwww\.)|(www\.))([a-z0-9]([a-z0-9]|(\-[a-z0-9]))*\.)+(com|it|net|uk|de)$/i; </code></pre> <p>(this last one for instance would validate only domain ending with .com/.net/.de/.it/.co.uk) <strong>New tlds always come out</strong>, so you would have to adjust you regex everytimne a new tld comes out, that's odd!</p> <hr> <p>In order to validate also the remaining part of an url you could add the remainig part at the end of the regex:</p> <p><code>var URLReg = /^((https?\://wwww\.)|(www\.))([a-z0-9]([a-z0-9]|(\-[a-z0-9]))*\.)+[a-z]+(\/[a-z0-9_\-\%\&amp;\?\.\=\+]*)*$/i</code>;</p> <p>It's still not perfect because somone could enter:</p> <p><a href="http://www.domain.com/????hello" rel="nofollow noreferrer">http://www.domain.com/????hello</a></p> <p>and it would validate ok, but now I'm tired, sorry! :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload