Regex For Url Validation Works In A Tester But Not In Javascript Function
So I have a URL validation that I am doing that is very relaxed and more or less just guiding the user to make sure they enter close to a URL. So I have got a regex that is doing
Solution 1:
In javascript: "\.com" === ".com"
(I think it's not you want.)
Try /regex/
instead of new RegExp("regex")
to create a regex.
Solution 2:
If you want to allow something like
adamthings.com
which has no leading http://
, https://
, ftp://
or www.
part, then you would need to list all allowed TLDs, such as com
, net
, etc., to prevent input such as
john.doe
to be valid as well.
Then your regex pattern should look like
^(?:https?:\/\/|ftp:\/\/)?(?:www\.)?(?!www\.)(?:(?!-)[a-z\d-]*[a-z\d]\.)+(?:com|net)
which checks just domain part of the link.
If you want to go further and check trailing part or link, then you need to add
(?:\/[^\/\s]*)*$
to the first part of pattern, however this (part of regex pattern) does not validate the url.
» Test link «
Post a Comment for "Regex For Url Validation Works In A Tester But Not In Javascript Function"