f



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.

0
3/30/2006 9:52:03 PM
comp.lang.javascript 38370 articles. 0 followers. javascript4 (1315) is leader. Post Follow

6 Replies
651 Views

Similar Articles

[PageSpeed] 19

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.
0
Michael
3/30/2006 10:52:58 PM
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.

0
Stephen
3/30/2006 11:52:48 PM
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
0
RobG
3/30/2006 11:58:08 PM
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.

0
Ouch
3/31/2006 2:45:54 AM
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.

0
Ouch
3/31/2006 2:50:43 AM
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
0
RobG
3/31/2006 12:02:02 PM
Reply:

Similar Artilces:

JavaScript not working in IE and Safari, works in Firefox fine.
I have a simple function that does submit for me: <script language="javascript" type="text/javascript"> function sort() { //selected item value from the drop down list var dud=window.document.statusForm.sortByStaff.value; window.document.statusForm.action="status.cfm?sortBy="+dud; window.document.statusForm.method="post"; window.document.statusForm.submit(); } </script> I have several drop down boxes (<select> element) in which i set onclick="sort()". Works perfectly in Firefox, and doesn't work...

Javascript include not working in Safari?
Hi everyone, If you visit this page in Safari, you will just see a paragraph of text: http://cabin.trailbehind.com/ In Firefox, the search box and the side menu get rendered properly (via javascript). Here is a test I did. The first Alert fires, but not the second Alert. Anyone have any thoughts on this mystery? window.onDomReady(onReady); function onReady() { var menuWidget = renderHomeMenu(); ... } //include via javascript include media/javascript/Widgets/ MenuWidget.js function renderHomeMenu() { alert(1) //NEVER FIRES menuWidget = new MenuWidget('col2', false); } ...

javascript within a javascript
Hello, I have a multi-frame page. The frames are named Frame_1, Frame_2 and Frame_3. Frame_1 has a drop down box. When a value is selected in this drop down box, Frame_2 is updated using an asp file (as I have to read from a back-end access database). In turn in the asp file, I have a Body onload tag to execute a javascript function within Frame_2. this function updates the contents of Frame_3. Till now this is working fine. Here is the problem. Frame_3 has a form with a radio button. When a radio button is selected, I want that a function be executed in Frame_3. However since conte...

Javascript not working with Safari/Firefox (Mac)
Navigation is not possible on a Mac with Safari/Firefox on: http://www.kerst.karelv.nl/frames/navigatie.html Why does the javascript script not work? -- JB Change: .... if (document.all) { menu.innerHTML=menucontent } .... to .... if ((document.getElementById)||(document.all)) { menu.innerHTML=menucontent } .... (Actually document.all is not needed at all, but let's keep the code as close as possible to the the unknown author's original). This is a war time script (Great Browsers' War), and its way to determine platforms is obsolete for 3-4 years at least. VK w...

Declaring a div using Javascript not working in Safari
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 getting hung up on the first line of code. I put an alert after that line and I never saw it in Safari but I did see it in Mozilla. Any Thoughts? Thanks, Andrew Here's the code: { document.write("<div id='orbitLayer' style='position:absolute; left:0px; top:"+document.all['topBanner'].offsetWidth+"px; width:"+document.all['topBanner'].offset...

Safari: targeting frames with a javascript event does not work.
The following code will work in Mac IE 5.1, but not in Safari 1.0 (v85). Anyone have any hints? <!-- Code begins --> <html> <body> <a href="text2.html" target="frameContent" onMouseDown="alert('Hi.');">Put text2.html in frameContent</a> <iframe width="200" height="200" src="text.html" frameborder="yes" scrolling="yes" name="frameContent"> </body> </html> <!-- Code ends --> Marc wrote: > The following code will work in Mac IE 5.1, but n...

Safari: referencing frames with a javascript even does not work.
The following code will work in Mac IE 5.1, but not in Safari 1.0 (v85). Anyone have any hints? <!-- Code begins --> <html> <body> <a href="text2.html" target="frameContent" onMouseDown="alert('Hi.');">Put text2.html in frameContent</a> <iframe width="200" height="200" src="text.html" frameborder="yes" scrolling="yes" name="frameContent"> </body> </html> <!-- Code ends --> show In article <d8d3a95b.0311232309.53aa09c...

Javascript not working: Safari, IE5.2, iCab
A particular web page using "JavaScript menus" displays no menus at all with Safari 1.2.1, or IE (last). With iCab 2.9.7 it displays something unclickable and wrong. I then tried a Mozilla or two and Opera, and those worked. http://www.topangabanjofiddle.org/ is the site in question. I wrote to them and they claim they're following standards (*which* standards was not stated). Is there a way to figure out who's responsible for this lack of functionality? Gary Shapiro wrote: > A particular web page using "JavaScript menus" displays no menus at...

Why won't cookies work with my javascript under Safari?
I have been testing some javascript for my professional society web page, and found that I couldn't get Safari to save any cookies. No matter what I assign to document.cookie, I get an empty string when I read back document.cookie. In researching this problem, I found a function on the Apple website to test for cookies. I found that the function sometimes tells me cookies are enabled and sometimes disabled, as follows: 1) Open the Safari Developers FAQ (http://developer.apple.com/internet/safari/safari_faq.html) 2) Push the "Got Cookies?" button. I get the reply "You g...

Javascript and IE? Javascript and C#?
While my question might be simple, the environment around it is terribly messy and so I will try to keep this clear and simple by only including the relevant code - however, as I will soon suggest, I worry that the problem isnt in what would seem to be the relevant code but instead is lost somewhere in the do-and-donts of the peripheral code. So at the end, i'll try to include all the affected code and you can see if any of its actually relevant. I know there is a lot going on... sorry, but I tried to write this clearly. So here is what I imagine to be relevant. I'm working with a pa...

javascript+safari
document.forms[0].submit I think the above function has problem with safari . It works fine on IE and Firefox. If yes please tell me the alternative. dishant On Sep 28, 9:57=A0pm, dishant <japra.dish...@gmail.com> wrote: > document.forms[0].submit > I think the above function has problem with safari . Why do you think that? Other than the missing call operator, it should work - do you have an example that doesn't? -- Rob dishant wrote: > document.forms[0].submit > I think the above function has problem with safari [...] 1. Go to <http://www.google.com/>. 2. ...

JavaScript Regex
Dear JavaScripters: I have not found quite the detail that I need for regexes. If you have a URL to a good site, please post it. In particular, how do I group subexpressions? I wish to parse a string to see if it is a valid fixed-decimal value. I start with /^ How do I check a set of characters optionally? I want to check for a sign character. The string might start with "+" or "-", but if it starts with neither, that is fine. Do I leave an empty choice in a parens set? Is it then ([+-]|) or [+-]? ? Then come...

Safari and javascript
How can you make a div visible and hidden in safari? document.all["div_submit"].style.display = "none"; Does not appear to work. Also, can anyone tell me how to turn on javascript error commenting in Safari? Right now, if javascript fails or errors, the browser just sits there and doesn't return any error message. -- Christine Christine Forber wrote: > How can you make a div visible and hidden in safari? > > document.all["div_submit"].style.display = "none"; > > Does not appear to work. of course ! document.all is Internet...

regex and Javascript
Hi - I've never tried using regular expressions with JS. Can someone help me with a simple one? I have a field that a user inputs that looks for an integer, or a some integers with a decimal then does some calculations and converts it to currency. Problem is, that if i have to recalculate, that field now has a "$" in it which throws off calculations. I'd like to look at the field and if there is a "$", strip it out, do the calculations, etc. So, something like: function getDollar() { myAmount = window.document.form.amount.value; //here is where i want to look f...

Web resources about - JavaScript and RegEx not working on Safari... - comp.lang.javascript

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

JavaScript - Wikipedia, the free encyclopedia
programming styles. JavaScript was formalized in the ECMAScript language standard and is primarily used in the form of client-side JavaScript ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
... editor - report abuse/spam - help the entire directory only in Tools/Obfuscators Description Top : Computers : Programming : Languages : JavaScript ...

Facebook File-Transfer App Pipe Relaunches, Now Works Via JavaScript, WebRTC
... allowed Facebook users to send and receive files up to 1 gigabit in size. A new version of Pipe debuted Monday, and the app now works via JavaScript ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

JavaScript
The Facebook SDK for JavaScript provides a rich set of client-side functionality for adding Social Plugins, making API calls and implementing ...

Senior Front-End Developer (Javascript and AJAX)
Company: Sometrics Category: Software Development Location: Los Angeles, CA How to Apply: jobs@sometrics.com Description: We are looking for ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken . blog.mozilla.org/research/2014/… ...

Resources last updated: 2/5/2016 10:17:39 AM