469,890 Members | 1,541 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

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

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
2 2727
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
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.

Similar topics

reply views Thread by gilgantic | last post: by
2 posts views Thread by mike | last post: by
1 post views Thread by Flack | last post: by
1 post views Thread by gryffin | last post: by
Kelicula
12 posts views Thread by Kelicula | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.