Syntax checking a comma-seperated list

Associate
Joined
23 Nov 2007
Posts
106
Hey guys,

I have a list of acceptable files in my web app.

doc, docm, rtf, xls, xlsx, csv, txt, pdf, zip, rar, xml

If the user changes this I want to check the syntax is correct. Was thinking of using regular expressions. I am using ASP and JS.

Cheers for any input,
Daveyboy
 
I don't know ASP's api too well, but surely there is a split function on String? If so, just split by ", " and then check if the extension is in the resulting array.

However this will not prevent people just simply renaming their files, so it would be more beneficial to investigate mime types.
 
exactly as Dj said, use the split function...

Code:
szUserFile = "file.doc"
szValidExtensions = "doc,docm,rtf,xls,xlsx,csv,txt,pdf,zip,rar,xml"

arrUserFile = split(szUserFile, ".")
arrValidExtensions = split(szValidExtensions, ",")

szExtension = arrUserFile(ubound(arrUserFile))

bExtensionValid = false
for i = 0 to ubound(arrValidExtensions)
    if cstr(arrValidExtensions) = cstr(szExtension) then
        bExtensionValid = true
    end if
next

if not bExtensionValid then
    response.redirect("errormessage.asp")
else
    ' business logic to do what you need with the file...
end if

It's also possible to do this in JS on the client side to save server time, but if people don't have javascript turned on they'd be able to upload anything they fancy = not good.
 
Last edited:
It can still be beneficial to do both client side validation and server side. If the client has JS enabled and it fails validation, it won't send another request to your server until it passes. Must always do server side validation because JS is not 100% reliable. :)
 
Back
Top Bottom