Re: JavaScript and RegEx not working on Safari...



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
.



Relevant Pages

  • Cells formatted for general and text show scientific format
    ... upload. ... The workbook is downloaded from amazon.com. ... I enter the Order Item ID number which is 14 characters, ... it displays in scientific format. ...
    (microsoft.public.excel.misc)
  • extract characters from filename
    ... I need a formula which will extract the last 4 characters from a file ... name (before the extension). ... Is it possible to format a cell so that it ...
    (microsoft.public.excel.worksheet.functions)
  • Re: How do I Save from MHTML .mht to HTML format only
    ... That will also add the .html extension ... You can choose the .html extension when you Publish to the ... The issue was the difference between html and htm on the index folder ... Double check that you did indeed upload to the ...
    (microsoft.public.publisher.webdesign)
  • Re: How do I Save from MHTML .mht to HTML format only
    ... The issue was the difference between html and htm on the index folder so I ... I also notice that your host says you have to use the .html extension for ... Double check that you did indeed upload to the ...
    (microsoft.public.publisher.webdesign)
  • Re: How do I Save from MHTML .mht to HTML format only
    ... The issue was the difference between html and htm on the index folder so I ... I also notice that your host says you have to use the .html extension for ... Double check that you did indeed upload to the ...
    (microsoft.public.publisher.webdesign)