javascript - How to use Regular expression to test for FIle Names with special characters -


i trying use open source image uploader: https://github.com/blueimp

the documentation says function match on file type can used match on file name also.

https://github.com/blueimp/jquery-file-upload/blob/7d46990486ff08acfc001b6368b09bce6712c2c2/js/jquery.fileupload-validate.js

can see way use match on , restrict special characters in file names?

here regex match characters want exclude. trying prevent end users using special characters in file names, instead of depending on training them. english concern in case.

[&~@#$^*()_+=/:?;\\|<>"',!%] 

here snipit source code (open source) evaluate it. full code available @ link above.

 // file upload validation plugin extends fileupload widget // file validation functionality: $.widget('blueimp.fileupload', $.blueimp.fileupload, {      options: {         /*         // regular expression allowed file types, matches         // against either file type or file name:         acceptfiletypes: /(\.|\/)(gif|jpe?g|png)$/i,         // maximum allowed file size in bytes:         maxfilesize: 10000000, // 10 mb         // minimum allowed file size in bytes:         minfilesize: undefined, // no minimal file size         // limit of files uploaded:         maxnumberoffiles: 10,         */          // function returning current number of files,         // has overriden maxnumberoffiles validation:         getnumberoffiles: $.noop,          // error , info messages:         messages: {             maxnumberoffiles: 'maximum number of files exceeded',             acceptfiletypes: 'file type not allowed',             maxfilesize: 'file large',             minfilesize: 'file small'         }     },      processactions: {          validate: function (data, options) {             if (options.disabled) {                 return data;             }             var dfd = $.deferred(),                 settings = this.options,                 file = data.files[data.index],                 filesize;             if (options.minfilesize || options.maxfilesize) {                 filesize = file.size;             }             if ($.type(options.maxnumberoffiles) === 'number' &&                     (settings.getnumberoffiles() || 0) + data.files.length >                         options.maxnumberoffiles) {                 file.error = settings.i18n('maxnumberoffiles');             } else if (options.acceptfiletypes &&                     !(options.acceptfiletypes.test(file.type) ||                     options.acceptfiletypes.test(file.name))) {                 file.error = settings.i18n('acceptfiletypes');             } else if (filesize > options.maxfilesize) {                 file.error = settings.i18n('maxfilesize');             } else if ($.type(filesize) === 'number' &&                     filesize < options.minfilesize) {                 file.error = settings.i18n('minfilesize');             } else {                 delete file.error;             }             if (file.error || data.files.error) {                 data.files.error = true;                 dfd.rejectwith(this, [data]);             } else {                 dfd.resolvewith(this, [data]);             }             return dfd.promise();         }      }  }); 

edit: things have tried:

thanks quick responses. things have tried here: many of these return the match it name preceeded invalid character. see http://regexr.com/3be9o don't want asdf&ghjik.jpg match valid.

i guess want a-z a-z 0-9 - _

[^&~@#$^*()_+=/:?;\\|<>"',!%]([\w]+\-*[\w]+)+(\.|\/)(gif|jpe?g|png)  ([^&~@#$^*()_+=/:?;\\|<>"',!%])?([\w]+\-*[\w]+)+(\.|\/)(gif|jpe?g|png)  ([\w]+\-+[\w]+)+(\.|\/)(gif|jpe?g|png)  [^&~@#$^*()_+=/:?;\\|<>"',!%]*(\.jpg)|[^&~@#$^*()_+=/:?;\\|<>"',!%]*(\.png)|  [^&~@#$^*()_+=/:?;\\|<>"',!%]*(\.gif)|[^&~@#$^*()_+=/:?;\\|<>"',!%]*(\.jpeg) 

as @nit pointed out in comment, white-listing rules better black-listing. means try specify what's allowed rather what's forbidden easy miss (did think of pound sign? non-english alphabets? utf characters in general?)

as start can use simple [\w\.\- ]

the \w metacharacter used find word character.

a word character character a-z, a-z, 0-9, including _ (underscore) character.

for explanation on good/bad file names in windows take @ this thread.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -