Well, soneone's gotta go first...
-
..may as well be me! This is perhaps a little cheeky to ask, but hey... my JavaScript skills are functional (shall we say) but I've never really mastered it... in particular, the use of arrays.... I have a FileUpload control, fu2, with the following attribute added in code behind:
fu2.Attributes.Add("onchange", "checkFile(this)")
which call this verification function in my JavaScript file:
function checkFile(what){
var source=what.value;
if (source!='') {
var i = source.lastIndexOf("\\");
var j = source.lastIndexOf(".");
var fName = source.substring(i+1,j);
var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
if ( ext=='doc' || ext=='docx' || ext=='pdf' || ) {
var regex = /^[A-Za-z0-9_ -]{1,50}$/;
if (!regex.test(fName)) {
alert('The file name contains illegal characters, or is too long\nPlease re-name the file using only alphanumeric charcters, hyphens, spaces and underscores\nand restrict it to a maximum of 50 characters\n' + fName);
}
} else {
alert('Please upload only files of the following types:\n .doc, .docx, .pdf');
}
}
}As you can see, this attempts to limit both the file types the user can upload, and also the file name. And actually, it all works well enough - but I know it could be written so much better... I feel that the "list" of allowable file types should be an array, and the test made against that, as well as the alert message built out of it - thus making the code that much easier to maintain if the list changes. Arrays.. hmm.. this is where I get stuck... plz, it's not urgentz, but if anyone is feeling generously inclined.... :-)
-
..may as well be me! This is perhaps a little cheeky to ask, but hey... my JavaScript skills are functional (shall we say) but I've never really mastered it... in particular, the use of arrays.... I have a FileUpload control, fu2, with the following attribute added in code behind:
fu2.Attributes.Add("onchange", "checkFile(this)")
which call this verification function in my JavaScript file:
function checkFile(what){
var source=what.value;
if (source!='') {
var i = source.lastIndexOf("\\");
var j = source.lastIndexOf(".");
var fName = source.substring(i+1,j);
var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
if ( ext=='doc' || ext=='docx' || ext=='pdf' || ) {
var regex = /^[A-Za-z0-9_ -]{1,50}$/;
if (!regex.test(fName)) {
alert('The file name contains illegal characters, or is too long\nPlease re-name the file using only alphanumeric charcters, hyphens, spaces and underscores\nand restrict it to a maximum of 50 characters\n' + fName);
}
} else {
alert('Please upload only files of the following types:\n .doc, .docx, .pdf');
}
}
}As you can see, this attempts to limit both the file types the user can upload, and also the file name. And actually, it all works well enough - but I know it could be written so much better... I feel that the "list" of allowable file types should be an array, and the test made against that, as well as the alert message built out of it - thus making the code that much easier to maintain if the list changes. Arrays.. hmm.. this is where I get stuck... plz, it's not urgentz, but if anyone is feeling generously inclined.... :-)
NeverHeardOfMe wrote:
I feel that the "list" of allowable file types should be an array, and the test made against that
<html><head><script>
var fileTypeAllowed = (function(){
var ar = arguments;
return function(arg){
for(var i = ar.length;i--;){
if(arg == ar[i]){return true;}}
return false;
};})('doc','docx','pdf');
alert(fileTypeAllowed('docx'));
alert(fileTypeAllowed('docxa'));
</script>
</head>
<body>
hi
</body></html>modified on Tuesday, September 14, 2010 12:44 PM
-
NeverHeardOfMe wrote:
I feel that the "list" of allowable file types should be an array, and the test made against that
<html><head><script>
var fileTypeAllowed = (function(){
var ar = arguments;
return function(arg){
for(var i = ar.length;i--;){
if(arg == ar[i]){return true;}}
return false;
};})('doc','docx','pdf');
alert(fileTypeAllowed('docx'));
alert(fileTypeAllowed('docxa'));
</script>
</head>
<body>
hi
</body></html>modified on Tuesday, September 14, 2010 12:44 PM
See... I knew it would be obvious.... :sigh: Thanks :thumbsup: (Sorry for not marking your answer as "Good" - done now!)
modified on Friday, July 23, 2010 4:19 AM
-
See... I knew it would be obvious.... :sigh: Thanks :thumbsup: (Sorry for not marking your answer as "Good" - done now!)
modified on Friday, July 23, 2010 4:19 AM
NeverHeardOfMe wrote:
See... I knew it would be obvious.... Thanks
There is nothing obvious about that code :-D
-
..may as well be me! This is perhaps a little cheeky to ask, but hey... my JavaScript skills are functional (shall we say) but I've never really mastered it... in particular, the use of arrays.... I have a FileUpload control, fu2, with the following attribute added in code behind:
fu2.Attributes.Add("onchange", "checkFile(this)")
which call this verification function in my JavaScript file:
function checkFile(what){
var source=what.value;
if (source!='') {
var i = source.lastIndexOf("\\");
var j = source.lastIndexOf(".");
var fName = source.substring(i+1,j);
var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
if ( ext=='doc' || ext=='docx' || ext=='pdf' || ) {
var regex = /^[A-Za-z0-9_ -]{1,50}$/;
if (!regex.test(fName)) {
alert('The file name contains illegal characters, or is too long\nPlease re-name the file using only alphanumeric charcters, hyphens, spaces and underscores\nand restrict it to a maximum of 50 characters\n' + fName);
}
} else {
alert('Please upload only files of the following types:\n .doc, .docx, .pdf');
}
}
}As you can see, this attempts to limit both the file types the user can upload, and also the file name. And actually, it all works well enough - but I know it could be written so much better... I feel that the "list" of allowable file types should be an array, and the test made against that, as well as the alert message built out of it - thus making the code that much easier to maintain if the list changes. Arrays.. hmm.. this is where I get stuck... plz, it's not urgentz, but if anyone is feeling generously inclined.... :-)
-
Just to let you know, you spelt characters wrong in the alert() function :)
NeverHeardOfMe wrote:
only alphanumeric charcters, hyphens, spaces
Ta.. I love typos - I do them all the time!