Many thanks RobG
"RobG" <rg***@iinet.net.au> wrote in message
news:43**********************@per-qv1-newsreader-01.iinet.net.au...
Xam wrote:
Hello everybody
Do you know of a javascript routine that can warn if there are any
pre-defined invalid chars in the filename of an INPUT file box before it
is
submitted with the submit button.
The process would be:
a) User clicks the INPUT File's Browse button to select the file from
their
computer.
b) The path and filename appear in the INPUT file's textbox.
c) The user clicks the INPUT submit button. Before the form submission is
fired, a javascript function checks that the filename (not any of the
path)
doesn't contain any invalid chars like &, ', ", etc
The following will return the filename from a path defined using either
'\' or '/' delimiters where 'path' is the full file path:
var fileName = path.match(/[^\\\/]+$/)[0];
Mac OS pre-X I think used ':' as a delimiter so that may be an issue.
To test the resulting fileName, you can define allowed characters and
remove them all, then whatever are left are the not-allowed ones.
Or you can define the not-allowed characters and see if you can find
any. Using the POSIX portable character set (0-9, a-z, A-Z, '.', '_',
and '-'.) as the allowed characters and using the first algorithm:
var okChars = /[\[a-z\]\[A-Z\]\d-_\.]/g;
var naChars = fileName.replace(okChars,'');
if (naChars && naChars.length){
// filename has not allowed characters
}
You have to do all this on the server also, because your script may not
run before the file is sent. Make sure you have on-screen help to let
users know that you don't allow certain characters.
--
Rob