473,385 Members | 1,292 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,385 software developers and data experts.

JavaScript and RegEx not working on Safari...

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..
// ok files with proper extension
var reOKFiles = /^([a-zA-Z].*|[1-9].*)\.(doc|DOC|pdf|PDF|rtf|RTF)$/;

//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");
window.document.myForm.myDocument.focus();
return (false);

TIA.

Mar 30 '06 #1
6 5792
On 30/03/2006 22:52, Ouch! wrote:
The script works on IE and Mozilla but fails on Safari on the MacOSX.
Care to be more specific? Those of us that can't test code on that
platform directly can't help you without a detailed error description.

[snip]
var reOKFiles = /^([a-zA-Z].*|[1-9].*)\.(doc|DOC|pdf|PDF|rtf|RTF)$/;
var filenamePattern = /^[a-z1-9].*\.(doc|pdf|rtf)$/i;

is much simpler, and would accept other, more usual (though valid)
capitalised forms of the extension.
//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");
window.document.myForm.myDocument.focus();
return (false);


I would write this as:

var uploadDocument = document.forms.myForm.elements.myDocument;

if (uploadDocument.value
&& !filenamePattern.test(uploadDocument.value)) {
alert('You tried to upload an invalid file type for'
+ ' Criteria 1.\nPlease try again.');
if (uploadDocument.focus) {uploadDocument.focus();}
return false;
}

though the reference to the form control could be reduced under certain
conditions. For example, by passing a reference to the form element
(presumably this code is part of a function called from the submit event
listener) and storing a reference to the elements collection if multiple
elements are being validated simultaneously.

It probably wouldn't make much of a difference, but feature testing the
focus method isn't a bad idea.

Please follow-up by repeating the error message you receive, and what
might be triggering the error. Finally, don't use tabs when posting to
newsgroups; use spaces to indent (preferably never more than four per
level).

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Mar 30 '06 #2

Ouch! wrote:
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..
// ok files with proper extension
var reOKFiles = /^([a-zA-Z].*|[1-9].*)\.(doc|DOC|pdf|PDF|rtf|RTF)$/;

[a-zA-Z].*

Taking the above in isolation, it reads: a single alphabetic character
followed by any number of any characters whatever.

Assuming that you want to accept:

an alphanumeric string dot range-of-extensions

I suggest: /^[a-z1-9]+\.(doc|pdf|rtf)$/i;

--
S.C.

Mar 31 '06 #3
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
Mar 31 '06 #4
Dear Mike, Spethan, and RobG:

Thank you for all the replies. I will look over each post to see what
my next steps are. Also, I may not have been 100% accurate w/ my
original posting. Certian users, mainly MacOS users, see the alert
message "Please try again, you tried to upload an invalid file type
for CRITERIA 1" when uploading a valid file name such as myfile.doc.
Users on Windows Platform using Mozilla and IE only see the alert
message when they try to upload an invalid file format.

Again, thanks for your help.

Mar 31 '06 #5
Dear Mike, Stephan, and RobG:

Thank you very much for all the replies. I will look over each post to
see what my next steps are...At the very least it looks like I need to
refine my regular expression.

Also, I may not have been 100% accurate w/ my original posting.
Certian users, mainly MacOS users, see the alert message "Please try
again, you tried to upload an invalid file type for CRITERIA 1" when
uploading a valid file name such as myfile.doc.

Users on Windows Platform using Mozilla and IE only see the alert
message when they try to upload an invalid file format.

Again, thanks for your help.

Mar 31 '06 #6
Ouch! wrote:
Dear Mike, Spethan, and RobG:

Thank you for all the replies. I will look over each post to see what
my next steps are. Also, I may not have been 100% accurate w/ my
original posting. Certian users, mainly MacOS users, see the alert
message "Please try again, you tried to upload an invalid file type
for CRITERIA 1" when uploading a valid file name such as myfile.doc.
Users on Windows Platform using Mozilla and IE only see the alert
message when they try to upload an invalid file format.


I've tested it now on Mac OS 10.2.8 and 10.4.5 and as I suspected, Mac OS,
being UNIX-based, has file paths like:

/Users/fred/Documents/fred.doc

Your regular expression doesn't allow leading forward slashes - the same
thing will happen with other UNIX-based OSs like Linux I expect.

You can try to just test the extension (as suggested in my other post), but
that doesn't really check the file format anyway.
--
Rob
Mar 31 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mr.Clean | last post by:
I am working on modifying a syntax highlighter written in javascript and it uses several regexes. I need to add a language to the avail highlighters and need the following regexes modified to...
1
by: IkBenHet | last post by:
Hello, I found this script to create a simple rich text form (http://programmabilities.com/xml/index.php?id=17): <html> <head> <title>Rich Text Editor</title> </head> <body>
2
by: drew197 | last post by:
I am a newbie. This is someone else's code which I have to modify to work in Mozilla and Safari. The changes I made so far have allowed it to work in Mozilla but not Safari. It seems to be...
2
by: vendredi5h | last post by:
Hello, Yesterday I spent a lot of time to find why my javascript script was not working on Safari. I finaly found a solution but not the reason. And I'd like to know if someone could tell me if...
7
by: petermichaux | last post by:
Hi, I have tried the following based on suggestions of the best way to insert JavaScript into a page. This is instead of using eval(). Unfortunately IE says "unexpected call to property or...
2
by: darren | last post by:
I have a small Javascript problem with that mutch love web browser safari, I tested the code on all other browsers PC (Win) and Linux and IE on the mac and it seams to work ok, but for some reason...
13
by: Zwerfkat | last post by:
When running the code below, all browsers are showing "Hello World" except Safari which cannot match the regular expression. See also http://www.testabc.nl/safari/test.html When I change *? into...
9
by: kummu4help | last post by:
can anyone give me a regex to validate the password with following conditions hope i am clear. i tried with ctype_alnum() function in php but it is accepting if all characters or either...
1
by: npm | last post by:
Hi I've checked out this page (and it works fine) in FF, IE, Opera, Chrome and Safari: http://www.w3schools.com/dom/tryit.asp?filename=try_dom_list_loop But when I try and tweak it for a site...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.