how can I validate an Input type file so that the name of the file I collect does not have special characters

1

Try using the html5 validation to tell it to accept only lowercase letters but it does not work. I am using the following to take the file and the following script to send it to the php where it will be processed.

<input id='file-0d' class='file' type='file' name='pdf' accept='application/pdf' required data-toggle='tooltip' data-placement='right' title='Selecciona un Archivo PDF con un nombre en minúsculas y sin caracteres especiales como: *#%@.,ñ '>

the script is this:

$(document).ready(function() {
$(document).on('submit', '#formsubpdf', function() { 

    var data = $(this).serialize();  

    $.ajax({  
        type : 'POST',
        url  : '../subirpdf.php',
        data:  new FormData(this),
        contentType: false,
              cache: false,
        processData:false,

        success :  function(data) {  
            $('#formsubpdf')[0].reset();
            //$("#ofertastrab").html(data);
             if (data=="1") {

          alert ('Ocurrio un problema al momento de guardar por favor intente de nuevo mas tarde ');    
        }else{alert ('Subido con Exito');}  
        }
    });
    return false;
})

});

    
asked by ivanrangel 03.07.2017 в 22:47
source

1 answer

0

good look with ajax would be something like this:

var isValid=(function(){
  var rg1=/^[^\/:\*\?"<>\|]+$/; // caracteres prohibidos \ / : * ? " < > |
  var rg2=/^\./; // cannot start with dot (.)
  var rg3=/^(nul|prn|con|lpt[0-9]|com[0-9])(\.|$)/i; // Nombres prohibidos para archivos
  return function isValid(fname){
    return rg1.test(fname)&&!rg2.test(fname)&&!rg3.test(fname);
  }
})();

isValid('file name');

on the side of PHP I think it would be something like that but you can improve it:

if(preg_match('/^[a-z0-9-]+\.ext$/', $file)) {
    // .. upload
} else {
    echo 'The file "' . $file . '"was not uploaded. The file can only contain "a-z", "0-9" and "-". Allso the files must be lowercase. ';

}
    
answered by 03.07.2017 / 23:16
source