Forum Moderators: open

Message Too Old, No Replies

Validate Filename

         

andrewsmd

9:38 pm on Jul 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to validate a file name with js. I need to make sure that there are no &'s in the file name because it will cause issues with a link later. The problem I am running into is, all I need to check is the file path, but old versions of ie and windows don't copy the file to a temporary path so if there is an & in the file path, then it throws an error when it shouldn't. Here is an example the path is

c:\users\user\a folder with &\theFileNameHere.pdf.
The file name is actually valid but my code is throwing an error because of the & in the previous folder. I tried doing something like a string split but I cannot seem to get it to work. Does anyone have any ideas for me? Here is the JS I use now

tempInput is the value that I mentioned above

if (tempInput.indexOf("&") == -1 && tempInput.indexOf("%") == -1) {
//we throw the error here

Fotiman

1:11 pm on Jul 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could use the lastIndexOf method to find the file separator \. Everything to the right of that is the actual file name. So maybe something as simple as this:

if (tempInput.lastIndexOf("&") <= tempInput.lastIndexOf("\"))

andrewsmd

1:28 pm on Jul 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll try that out and get back you. Thanks,

andrewsmd

4:13 pm on Jul 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok new problem. In IE 7 only, when I do a lastindexof on a filepath, it returns the wrong one. I.e. if I have c:\folder &\file & stuff.pdf, it is returning the first occurrence. It is working in all other browsers including IE 8. Oh the curse words I would love to deliver to those who were responsible for IE 7. Any other ideas? Here is what I was using.

if ((tempInput.lastIndexOf("&") > tempInput.lastIndexOf("\\")) || (tempInput.lastIndexOf("%") > tempInput.lastIndexOf("\\"))) {
arguments.IsValid = false;
} //if tempInput.lastIndexOf("&") >

rainborick

9:20 pm on Jul 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Any chance you could change the back-end software that handles the upload so that it generates its own file name for local storage purposes and the user's file name is stored only as a title for display to the user? I sympathize with your problems working with file uploads and JavaScript, but there are times when only the server-level software is reliable. Just a thought.

andrewsmd

9:41 pm on Jul 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That I understand, and is how I would have built this, had I been the person building the software. This person didn't even check for characters like that. Personally, I wouldn't have even used the filename to name files, you have a table with filenames and then their name on the server side which you create. I'm still working on it. I will post back with my fix.

Fotiman

2:06 pm on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Ok new problem. In IE 7 only, when I do a lastindexof on a filepath, it returns the wrong one.

Huh? I don't know how that's even possible. I just tried this and it works fine in IE7.

<!DOCTYPE html>
<html>
<head>
<title>lastIndexOf Test</title>
</head>
<body>
<script>
var filePath = "c:\\folder &\\file & stuff.pdf",
fileSeparator = "\\",
fileNameStart = filePath.lastIndexOf(fileSeparator), // 11
illegalChars = ['&', '%'],
isValid = true,
idx,
i,
n;
for (i = 0, n = illegalChars.length; i < n; i++) {
idx = filePath.lastIndexOf(illegalChars[i]);
if (idx > fileNameStart) {
isValid = false;
alert(idx)
break;
}
}
</script>
</body>
</html>

andrewsmd

2:45 pm on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found this only to be on XP SP1. Of course, the clients of the website use that. I ended up just validating on the server side. Trying to avoid rebuilding it just ended up costing me more time, instead of just doing it in the first place.