-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

3
  • Possible duplicate of Javascript URL validation regex Commented Aug 1, 2016 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 Commented Aug 1, 2016 at 17:55
  • Just use a try catch block around new URL() Commented Aug 1, 2016 at 18:01

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

7 Comments

Can this be included as another condition in my above function or will this only satisfy the fileshare structure?
Currently, it will only validate the file share path. We need to consolidate both.
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
I have updated the answer above as an consolidated regular expression. Please check and let me know.
I gave this a try in a fiddle and it doesnt seem to return either true or false. Did I miss something? jsfiddle.net/carlhussey/osq6an4n/1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.