-3

Possible Duplicate:
reject if file name contains non english character

how to check file name contain non english character like Cisco-S11-POA1800005815-Inv04736851-100919重做没有.pdf i need to avoid non english character

Community
  • 1
  • 1
vijay
  • 11
  • 1
  • 3
  • Define what you mean by a "non-English" character. – Stephen C Jan 27 '11 at 06:19
  • 3
    Duplicate of [reject if file name contains non english character](http://stackoverflow.com/questions/4790950/reject-if-file-name-contains-non-english-character), asked by the same user yesterday and no answer accepted. @vijay: re-asking your question is definitely not the way to get positive help. – Phrogz Jan 27 '11 at 06:22

2 Answers2

4
// Disallow anything but a-z, 0-9, underscore, period, hyphen, and comma
var disallowed = /[^a-z0-9_.,-]/i;
if (disallowed.test( myFileName )){
  // do something here
}

Add to the list of allowed characters as you see fit, but ensure that the hyphen is at the end of the list, or else escape it with \-.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • 1
    This may or may not be correct, depending on what @vijay means by "non-english character". – Stephen C Jan 27 '11 at 06:20
  • can u explain me what this /[^a-z0-9_.,-]/i will do – vijay Jan 27 '11 at 06:25
  • @vijay It will look to see if there is any character (`[...]`) that is _not_ (`^`) a letter (`a-z`), a number (`0-9`), or one of the other characters listed there. The `i` at the end makes it case-insensitive. – Phrogz Jan 27 '11 at 06:26
  • if i get any other special characters in my filename like #@$ then? thanks for u r response – vijay Jan 27 '11 at 06:30
3

The proper answer depends on knowing your character set.

But a regular expression like this:

var re = /[\x80-\xFF]/;
if (re.match(...)) {
    alert("File name contains non-ASCII character!");
}

is a quick answer.

Note that it will also complain if your file name contains any special symbols.

Maybe a whitelist, saying only the allowed characters, is a better option, something like this?

var re = /[^A-Za-z0-9_,.-]/;
if (re.match(...)) {
    alert("File name contains non-ASCII character!");
}
Mikel
  • 24,855
  • 8
  • 65
  • 66