473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 2865
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: gilgantic | last post by:
I get an "Invalid file" in my MIDlet when I tried to read a .txt file, using Connection.open(). My phone is a Nokia 3595 with mMode technology by AT&T. Here is my source code in my MIDlet: try...
2
by: mike | last post by:
I had a form like below that validated that a file was there before it would submit. <form name="attach" method="POST" action="run_this_pgm.cfm" enctype="multipart/form-data"...
1
by: Flack | last post by:
Hello, I 'm using drag-n-drop and if the user drops a file, I want to make sure that it is either an Excel file or a plain text file. Is the right way to do this to check that the last three...
14
by: tranky | last post by:
Hi, i'm italian...so...excuse me for my english. I've a little problem....in what manner i can check a textbox for know if it contain only character from A-Z (a-z), numbers (0-9), and underscore...
1
by: gryffin | last post by:
im trying to do file extension checking but its not working :( i have the following in the head <script language="JavaScript"> extArray = new Array(".jpg", ".png", ".bmp"); function...
3
by: technotrix411 | last post by:
I m trying to use a iterative function for error checking to open a file? the problem is that once it enters the error loop , it does not come out .. i think its due to the object (ifile) .. which...
1
by: deerchao | last post by:
I used these codes to provide a default "Save As" filename for browsers: Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); Response.ContentType =...
12
Kelicula
by: Kelicula | last post by:
I am trying to ensure that only files of a certain type can be uploaded. Why doesn't this work?? (It's a code snippet..not the whole file) if(my $file = $q->param('avatar')){
10
by: Brent | last post by:
I have a list of company names (say, IBM, Corning, General Motors, and another 5,000 of them). If I take a body of text, a news article, for instance, and I want to see which company names...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.