Search code examples
javascriptjqueryruby-on-rails-3.1uploadify

How to check file name for non-ASCII characters via JS?


I am using jQuery Uploadify to upload a file directly to S3. Everything works so far.

What I need now is to validate the file via JS. To make sure that all characters are ASCII compatible.

How can this be done?


Solution

  • The printable ASCII characters start at 0x20 (space) to 0x7E (~). The RegExp to match this range is: [\x20-\x7E].

    So, the final code:

    var filename = "foo.bar";
    if(/^[\x20-\x7E]+$/.test(filename)){
        //Valid, continue
    } else {
        //Invalid, notify the user
    }