|
|
Download now
View the source of
the control.
About the code
This code was written to solve a specific problem on a specific intranet applicaiton.
Before deploying it, you should test it against your specific requirements,
including non-Internet-Explorer browsers.
Issues with
Firefox and File Upload
|
|
|
Why another Validation control?
June 2004
The 'built-in' ASP.NET RegularExpressionValidator control has a BiG pRoBLeM - it's
CASE SENSITIVE.
For example, this page explains
"CAUTION Regular expressions work somewhat differently in JavaScript than they
do in the .NET framework. So, in certain circumstances, the client-side validation
code used for matching regular expressions might return different results than the
server-side validation code. Even worse, a valid .NET regular expression might generate
a JavaScript error. For example, the syntax for using regular expression options inline,
such as the i option [for case sensitivity], differs between JavaScript and the .NET classes."
Searching for solutions on the web, most recommendations (like this page)
were just to set EnableClientScript="false" and let the validation occur at the server.
The problem with that solution is that when the validation is designed to prevent
data being submitted that could be time-consuming (such as a file-upload), waiting
to validate on the server is too late!
This MSDN article Uploading Files Using the File Field Control
glosses over this problem, describing a File Upload solution that validates for mp3 and MP3
but not Mp3 or mP3...
For example, this Regex (based on ideas from here)
^([a-zA-Z]\:|\\)\\([^\\]+\\)*[^\/:*?<;>;|]+(\.txt|\.pdf|\.htm|\.html|\.zip)$
will restrict the uploaded file extension to the list - but only in lower-case!
If the user has (for some reason) a file named Index.Html, it won't be allowed.
The InsensitiveRegularExpressionValidator code creates a subclass
of BaseValidator which mimics the RegularExpressionValidator in the way it
operates, but includes the 'case-insensitive' option both:
on the client, with the 'i' option new RegExp(val.validationexpression, 'i'); /* Case insensitive on the Client-side */
and on the server, System.Text.RegularExpressions.RegexOptions.IgnoreCase
NOTE: It even uses 'common' functions in the Framework Javascript include, so
make sure that is installed and available on your pages.
Firefox and File Upload
The InsensitiveRegularExpressionValidator was originally developed for
validating <input type="file">
Firefox is tricky in that it when it sends uploaded files
it doesn't tell the server the 'full path' (eg. C:\Documents and Settings\myUser\ Desktop\myFile.ext)
just the filename (eg. myFile.ext).
This means the Regular Expression above will fail
on the server when Firefox's FORM variables are received
and processed. It might also fail on Mac's even when
using Internet Explorer...
On the whole, this is a "good thing" for privacy/security
- a web server doesn't
need to know about the directory structure on my PC - but
if you're expecting file uploads from Firefox/Mozilla users,
make sure your validation doesn't prevent them from doing so!
|
|
Useful links
View the code!
C# code
Validator
Control Samples
The Longhorn SDK contains samples for building controls that will work in Netscape!
The Basics of Creating Server Controls
[aspAlliance] series of articles about server-controls
A Look at ASP.NET's Adaptive Rendering
[4guysfromrolla] article about the lifecycle of pages and controls
Regular
Expression Validator Control Sample
The Insensitive RegularExpression Validator is based on this information
|