Re: JavaScript and RegEx not working on Safari...
- From: RobG <rgqld@xxxxxxxxxxxx>
- Date: Thu, 30 Mar 2006 23:58:08 GMT
Ouch! said on 31/03/2006 7:52 AM AEST:
I am using Regular Expressions and Javascript to validate a form,
specifically I want to make sure that if they try to upload a file that
it has a proper name w/ certain extensions (doc,pdf, rtf). The script
works on IE and Mozilla but fails on Safari on the MacOSX. Here is my
code..
In addition to what Mike said, remember:
1. Mac users don't have to use file extensions, particularly pre OS X.
2. A particular file extension is no guarantee that the file is of
a particular format.
3. Mac OS 9 (and earlier) used ':' as the separator for paths
rather than '/' or '\'
The above may be relevant, or not. :-)
// ok files with proper extension
var reOKFiles = /^([a-zA-Z].*|[1-9].*)\.(doc|DOC|pdf|PDF|rtf|RTF)$/;
You appear to be testing the entire file path and allowing many characters that aren't valid on most file systems. You also disallow a path starting with a character other than a letter or number, but there are many systems with paths that start with other characters, e.g. '/' for UNIX (and similar) systems.
If you want to test just the file name, test just the file name using criteria that are acceptable to your server. But even that is likely to fail, so maybe just test the extension:
var reOKFiles = /\.(doc|pdf|rtf)$/i;
The vast majority of users will use the 'browse' button so the path is filled in automatically. It is only relevant for their system, you really don't care what it is.
//where i check for the file...
if(window.document.myForm.myDocument.value != ""){
var fileStr = window.document.myForm.myDocument.value;
if(!reOKFiles.test(fileStr)){
alert("Please try again, you tried to upload an invalid file type
for CRITERIA 1");
Perhaps you should say something along the lines of:
"This file doesn't appear to be the right format, please upload
only if you are certain it is in Microsoft Word or rich text
format, or Adobe PDF format."
The bottom line is that the extension does not define or guarantee the file format. Many systems (including Macs) now hide the file extension by default, so users may become confused if you refer to it.
Any testing you do at the client using JavaScript will fail in some circumstances, so why not just upload the file and test it at the server? What are the ramifications of a user uploading a file in the 'wrong' format? What is there in your script that stops them from uploading it anyway?
Some simple work arounds: they can turn off scripting, change the extension without changing the file format or spoof the file submission and send the file anyway. There are probably more...
Sorry to be so negative, especially on Friday. :-)
--
Rob
.
- Follow-Ups:
- Re: JavaScript and RegEx not working on Safari...
- From: Ouch!
- Re: JavaScript and RegEx not working on Safari...
- From: Ouch!
- Re: JavaScript and RegEx not working on Safari...
- References:
- JavaScript and RegEx not working on Safari...
- From: Ouch!
- JavaScript and RegEx not working on Safari...
- Prev by Date: Re: enumerator not working in firefox
- Next by Date: Re: Printing in one line like in Java or C/C++
- Previous by thread: Re: JavaScript and RegEx not working on Safari...
- Next by thread: Re: JavaScript and RegEx not working on Safari...
- Index(es):
Relevant Pages
|