Reputation: 179
I have an input of type file, my question is: after I select the file, can the file name be limited to a certain number of characters ?
Upvotes: 2
Views: 5923
Reputation: 13866
Do you mean like this?
var limit = 8;
var fileName = document.getElementById("file-name-field").value();
// get both parts
var fileNameExtension = "." + fileName.split(".").pop();
var fileNameBase = fileName.substring(0, fileName.length()-fileNameExtension.length());
if(fileNameBase.length()>limit){
// now limit it and set it as fileName
fileName = fileNameBase.substring(0, limit) + fileNameExtension;
}
Upvotes: 2
Reputation: 3962
Please try following JavaScript to check length of the file name.
function valueCheck()
{
var filePath = document.getElementById("file").value;
var fileName = filePath.replace(/[^\\\/]+$/, "");
if(fileName !=null && fileName.length >10)
{
alert('Filename if bigger');
}
}
Demo URL : http://jsfiddle.net/BhaveshKachhadiya/6EPvg/6/
Upvotes: 0
Reputation: 56501
You can get the file name using
var filename = document.getElementById('file-id').value;
filename = filename.replace(/^.*[\\\/]/, '');
But limitation in sense, after uploading the file you can get the file name using above approach.
Then you can have
if (filename.length < 100 ) {
//do something
}
FYI: Evert thing happens only after the file being uploaded in client side. There is no use in limiting the filepath before uploaded to server.
Upvotes: 2
Reputation: 44823
Most browsers don't allow modification of the value attribute of file fields. It's a security hole, because it would allow a malicious page to retrieve an arbitrary file using a hidden field.
Upvotes: 0