Reputation: 891
I have a textarea which takes links (web, intranet or shared folder in other PC) to be validated. I have used this regular expression: ^(http:\/\/|https:\/\/|ftp:\/\/|www.){1}([0-9A-Za-z]+\.)
. My code is as below.
var isUrlValid = false;
var urlRegex2 = new RegExp(
"^(http:\/\/|https:\/\/|ftp:\/\/|www.){1}([0-9A-Za-z]+\.)");
isUrlValid = urlRegex2.test(linkPathVal);
This doesn't validate links like:
http://abcserverName/Home/Index#
https://wiki.abc.in/abc/xyz
But it validates others like http://google.com, http://www.google.com etc. May I know where I went wrong? I know it's simple but I am not that knowlegable with Regex.
Thanks.
Upvotes: 0
Views: 66
Reputation: 11461
Here's a visualization of your regular expression. From this it should be easy to see where you went wrong:
You aren't allowing for anything other than alphanumerics and periods after the protocol string. You may also want to allow for slashes, hash signs, querystring characters and the like.
In fact your regular expression just seems to be checking if it starts with a protocol string (or www), is followed by some alphanumerics, and then a period. It will match up to the first period in your example https://wiki.abc.in/abc/xyz
. It won't match http://abcserverName/Home/Index#
because there is no period and your regular expression requires one.
Upvotes: 4