By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,600 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,600 IT Pros & Developers. It's quick & easy.

Checking for invalid chars only in the filename of an INPUT File box

P: n/a
Xam
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

Its the checking of only the filename not the whole path that is causing me
headaches.

Thanks XAM
Sep 30 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Oct 1 '05 #2

P: n/a
Xam
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
Oct 1 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.