I am trying to use this open source image uploader: https://github.com/blueimp
The documentation says that the function to match on File Type can be used to match on file name also.
Can anyone see a way to use this to match on and restrict special characters in the file names?
Here is the RegEx that will match the characters that I specifically want to exclude. I am trying to prevent end users from using special characters in file names, instead of just depending on training them. English is the only concern in this case.
[&~@#$^*()_+=/:?;\\|<>"',!%]
Here is the snipit from the source code (open source) that would evaluate it. Full code available at the link above.
// The File Upload Validation plugin extends the fileupload widget
// with file validation functionality:
$.widget('blueimp.fileupload', $.blueimp.fileupload, {
options: {
/*
// The regular expression for allowed file types, matches
// against either file type or file name:
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
// The maximum allowed file size in bytes:
maxFileSize: 10000000, // 10 MB
// The minimum allowed file size in bytes:
minFileSize: undefined, // No minimal file size
// The limit of files to be uploaded:
maxNumberOfFiles: 10,
*/
// Function returning the current number of files,
// has to be overriden for maxNumberOfFiles validation:
getNumberOfFiles: $.noop,
// Error and info messages:
messages: {
maxNumberOfFiles: 'Maximum number of files exceeded',
acceptFileTypes: 'File type not allowed',
maxFileSize: 'File is too large',
minFileSize: 'File is too 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: Some things I have tried:
Thanks for the quick responses. Some things I have tried here: Many of these return the the match even it it the name is preceeded by an invalid character. See http://regexr.com/3be9o I don't want asdf&ghjik.jpg to match as valid.
I guess I really 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)