-5

I need to validate folder path with having forward(/)slash

regex or javascript code should validate and allow below format path only.

ex1: "D:/dir1/dir2/dir3"

ex2: "c:/dirname1/somedir/dirname/batch.log"

ex3: "E:/user/desktop/somedir/fname.csv"

  • 5
    Possible duplicate of [File path validation in javascript](https://stackoverflow.com/questions/16231210/file-path-validation-in-javascript) – I'm Limit Sep 17 '19 at 04:55

2 Answers2

0
regx: '/^(?:[\w]\:|\/)(\/[A-z_\-\s0-9\.]+)+/g'

With version: '^(?:[\w]\:|\/)(\/[A-z_\-\s0-9\.]+)+\.(txt|gif|pdf|doc|docx|xls|xlsx)$'

Expalnation :^(?:[\w]:|/) -- Begin with x:/ or //

[A-z_-\s0-9.] -- valid characters are a-z| 0-9|-|.|_ (you can add more)

(txt|gif|pdf|doc|docx|xls|xlsx) -- Valid extension (you can add more)

Divyesh patel
  • 967
  • 1
  • 6
  • 21
  • frustrateddeveloper, Above solution is working fine. but now I need colon( : ) optional. for ex "c:/dirname1/somedir/dirname/batch.log", or "c/dirname1/somedir/dirname/batch.log" could you please help me here to achieve this. – shashi keshari Sep 19 '19 at 07:28
-1

I think this will works:

 var pattern = "[(c|C|D|d|E|e)]:.*";
 var str = "E:/dir1/dir2/dir3";
 var result = str.match(pattern);
GnanaJeyam
  • 2,780
  • 16
  • 27