0

I have a form that lets the user type what they want their filename with extension to be when downloading a file, but I need to validate on the client side (doesn't contain [^/?*:;{}\\]+\\.[^/?*:;{}\\]+, stuff like that). The filename does not need to contain an extension, but can if they wish. I found this : Validate a file name on Windows But I am not fluent enough in jQuery to make it work.

Community
  • 1
  • 1
Howes
  • 799
  • 1
  • 7
  • 15
  • 4
    This doesnt really have anything to do with jQuery perse, its a javascript issue. Here you go, MDN docs for regexp https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp – Henrik Andersson Dec 31 '12 at 17:12
  • I'd suggest using a white-list (rather than a blacklist) of characters you'd allow, it's a little easier to manage that way. – David Thomas Dec 31 '12 at 17:18
  • @DavidThomas any reasonable file system has a blacklist, not a whitelist. Some don't even have either. Linux will let you insert control characters into the file name if the file system supports that. – John Dvorak Dec 31 '12 at 17:20
  • @Jan: I didn't say that's how it's most-often implemented in file systems, but I do believe that for validation purposes a white-list is easier to manage. But, of course, it's a personal choice and both approaches are (to my mind at least) perfectly valid. – David Thomas Dec 31 '12 at 17:21

2 Answers2

0

As my comment stated its not a jQuery issue its a javascript thing you're after.

You can go two ways, the easiest being

var myString = "hello world";
var regexp = /w+/
var match = regexp.exec(myString);
console.log(myString); //will print the world "hello"

The other would be using the Regexp Object.

Simply instead of using "literal regexp", if you would agree to call it that, use the object

var re = new RegExp("\\w+");
var match = re.exec(myString);
console.log(myString); //will print the world "hello"

This handy website is primary for Ruby regexp but its a great tool of creating regexps

Rubular

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
0

You can try the following code for this. This return the file names if valid. else it will return null

var filename = ''; // enter your file name here
alert(filename.match(/^\s*[a-z-._\d,\s]+\s*$/i))

If you want to exclude some characters mention those chars inside the square brackets as shown below

/^[^<>%$]*$/
Wolf
  • 2,150
  • 1
  • 15
  • 11
  • `příliš žluťoučký kůň.texťák` is not valid file name in your opinion? My file system (NTFS@W7) begs to differ :-) – John Dvorak Dec 31 '12 at 17:23
  • that looks like a made up language... ;) – Henrik Andersson Dec 31 '12 at 17:26
  • @limelights I think the same about Hungarian :-) see http://en.wikipedia.org/wiki/List_of_pangrams#Czech to verify it's a real language :-) – John Dvorak Dec 31 '12 at 17:30
  • @limelights "Jó foxim és don Quijote húszwattos lámpánál ülve egy pár bűvös cipőt készít." -- "My good foxterrier and don Quixote are making a pair of magic shoes by a 20-watt lamp." in Hungarian :-) – John Dvorak Dec 31 '12 at 17:33
  • Edited my code. If you just want to mention some characters to exclude you can use something as mentioned in my answer. – Wolf Dec 31 '12 at 17:44