-1

I am trying to validate some urls in an application I have and found a function to do so. I can pass the URL and it will return true or false if the url is valid according to the regex.

While this is working for the typical everyday URLs, we have some urls that are on fileshares that we need to allow to pass validation.

I am trying to find out either a good pattern to use for fileshares that can be built into the function below or a jQuery plugin that me include this validation if this already exists.

alert(isURL('\\server-123.somedomain.com\path\to\file.txt')); // Returns False

function isURL(str) {
  var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
  '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|'+ // domain name
  '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
  '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
  '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
  '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
  return pattern.test(str);
}

Edit:

I don't need it to match that exact path structure in the example shown. I just need a pattern that allowed for file shares in general. My question is specific to the file share structure URL and not the typical http/s/ftp as mentioned as such in the suggested post here

Community
  • 1
  • 1
SBB
  • 8,560
  • 30
  • 108
  • 223
  • Possible duplicate of [Javascript URL validation regex](http://stackoverflow.com/questions/18364404/javascript-url-validation-regex) – Haroldo_OK Aug 01 '16 at 17:54
  • 1
    @Haroldo_OK That just validates the typical (http/s/ftp) structure. I am looking for that in addition to file shares `\\fileshare-path\something.txt` – SBB Aug 01 '16 at 17:55
  • Just use a try catch block around `new URL()` – Ryan Aug 01 '16 at 18:01

1 Answers1

2

You may probably try with this RegEx,

^(https?:\/\/)?((([a-z\d]([a-z\d-]*[a-z\d])*)\.?)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3}))(\:\d+)?(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=-]*)?(\#[-a-z\d_]*)?$|^((\\(\\[^\s\\]+)+|([A-Za-z]:(\\)?|[A-z]:(\\[^\s\\]+)+))(\\)?)$

You can test this here: https://regex101.com/r/nE9mZ2/1

Hope this helps!

David R
  • 14,711
  • 7
  • 54
  • 72
  • Can this be included as another condition in my above function or will this only satisfy the fileshare structure? – SBB Aug 01 '16 at 17:58
  • Currently, it will only validate the file share path. We need to consolidate both. – David R Aug 01 '16 at 17:59
  • And to combine your regular expression with the one I provide above, all you need to do is to use a | (pipe) symbol in between – David R Aug 01 '16 at 18:33
  • I have updated the answer above as an consolidated regular expression. Please check and let me know. – David R Aug 01 '16 at 18:34
  • I gave this a try in a fiddle and it doesnt seem to return either `true` or `false`. Did I miss something? https://jsfiddle.net/carlhussey/osq6an4n/1/ – SBB Aug 01 '16 at 18:37
  • **There is nothing wrong with the regular expression**. Actually we need to take of the backslashes as Javascript tends to treat them differently, In our case we need to make our string as `'\\\\server-123.somedomain.com\\path\\to\\file.txt'` (Double quotes to have extra \\ and single quote needs to have an extra single quote) **I have updated your fiddle** : https://jsfiddle.net/osq6an4n/2/ – David R Aug 01 '16 at 19:23
  • Thanks David. I will just need to look into a way to encode these links then as they are stored in the database without the extra slashes that the function is looking for in order for it to work – SBB Aug 01 '16 at 19:56