-1
Good afternoon. I need to do a URL validation using javascript. I have the following regex:
/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@@!\$&'\
(\)\*\+,;=.]+$/gm
It turns out that if I type for example: www.test it validates. What I needed was for it to be validated too . com, . br, or any other extension with at least 2 characters. How can I add this to my regex?
Here has several options, and see that it is a regex more complicated than another, since validating URL with regex it’s not that simple. Anyway, I would take the second point out of the excerpt
(?:\.[\w\.-]+)+
(just staying(?:\.[\w\-]+)+
, or(?:\.\w+)+
, otherwise the regex will accept several points in a row, see (I didn’t see if there were more things to fix)– hkotsubo
Just out of curiosity, what if the person has an ending domain
.io
, or a domain with an end.eu
?– Guilherme Nascimento
The point is that
www.teste
is a valid URL in accordance with RFC 3986, as well asteste
,/home
,john@due
, etc. If you do not wish to validate these values, I advise you to edit the question and describe exactly what the valid values would be.– Woss
Possible duplicate of Regular expression to validate urls
– Vinicius Dutra