2
I have the following script in which I use a jquery to validate a regular expression that matches the filenames for upload:
$(document).on('change', "input[type='file']", function() {
var validaFile = $("input[type='file']").val();
if (validaFile != '') {
var regex = RegExp('^[a-zA-Z0-9-_.]+$');
if (!regex.test(validaFile)) {
$("input[type='file']").val('');
alert("O nome deste arquivo é inválido. Por favor renomeie o arquivo.");
return;
}
}
});
The point is that the code worked with Firefox Developer, but it doesn’t work with Chrome and Edge. Regardless of the file I enter will always give invalid file name.
What this regex should do?
– PauloHDSousa
I have an input on my site for uploading files. This regex does not handle the file type, only the name. Example: if the user tries to insert a file with the name Me,u! Arqu;Ivo will be an invalid filename and cannot be attached. Only az Az file will be attached _ -.
– Leonardo
Weird.. I tested it here and it worked. http://jsfiddle.net/yj78v/57/
– PauloHDSousa
Test a file Example: Test.txt and a T,es! te.txt ... Will return the same message
– Leonardo
@Paulohdsdare questioning him are the correct files that are entering If.
– Marconi
Correct, firefox dev works fine. if I enter a file with an invalid name it does not allow me, and if I put a valid name it allows. The problem is that Chrome does not work and even if I try to put a valid file, it says it is invalid
– Leonardo
Why
new RegExp('^[a-zA-Z0-9-_.]+$');
instead of/^[a-zA-Z0-9-_.]+$/
? Another thing has a hyphenating after the 9, you have to escape this hyphen so:/^[a-zA-Z0-9\-_.]+$/
or better/^[\w\d\-_.]+$/
– Guilherme Nascimento
Thanks for the tip expensive, but with the answer from below the problem has already been solved.
– Leonardo