Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Javascript works in firefox but not in IE explorer
Old 02-22-2008, 05:28 PM Javascript works in firefox but not in IE explorer
Novice Talker

Posts: 14
Name: Orion Tiller
Trades: 0
Any idea why this wouldn't work in explorer but will in firefox. I'm using it to validate my form.


<script language="javascript" type="text/javascript" src="validate.js"></script>
<form name="FormName" method="post" action="index.php" onsubmit="return validate(this);">



// validate.js

function validate(form) {
var valid = true;
var FirstErrorField = '';
var checkboxes = new Array();
var thisbox = '';

for(i in form.elements ) {
//alert( i );
try{
if( (form.elements[i].type == "checkbox") && (checkboxes[form.elements[i].getAttribute("name")] == undefined) ) {
thisbox = form.elements[i].getAttribute("name");
checkboxes[thisbox] = false;
for(j in form.elements) {
if( form.elements[j].type == "checkbox" ) {
if (form.elements[j].getAttribute("name") == thisbox ) {
normalField(form.elements[j].parentNode);
checkboxes[thisbox] = (checkboxes[thisbox] || (form.elements[j].checked));
}
}
}

if ( checkboxes[thisbox] == false ) {
for(j in form.elements) {
if( form.elements[j].type == "checkbox" ) {
if (form.elements[j].getAttribute("name") == thisbox ) {
highlightField(form.elements[j].parentNode);
}
}
}
valid = false;
}
}
else if(form.elements[i].type != "hidden") {
//if(form.elements[i].parentNode.parentNode.parentNode.getElementsByTag Name("div")[1].getElementsByTagName("label")[0].getAttribute("class") == "required") {
if(form.elements[i].getAttribute("class") == "required") {
if(form.elements[i].value == '') {
highlightField(form.elements[i]);
valid = false;
}
else if( form.elements[i].getAttribute("title") == "numeric" ) {
if ( (isNumeric(form.elements[i].value) == false) || ( form.elements[i].value.length < form.elements[i].getAttribute("maxLength") ) ) {
highlightField(form.elements[i]);
valid = false;
}
else {
normalField(form.elements[i]);
}
}
else if( form.elements[i].getAttribute("name") == "EMAIL" ) {
if ( validateAddress(form.elements[i].value) == false ) {
highlightField(form.elements[i]);
valid = false;
}
else {
normalField(form.elements[i]);
}
}
else {
normalField(form.elements[i]);
}
}
}
}
catch( error ) {
}
}

if( valid == false ) {
alert( "Please fill in all required form fields with valid data." );
}
return valid;
}
function highlightField(field) {
field.style.backgroundColor = '#FF6666';
field.style.color = 'white';
}
function normalField(field) {
field.style.backgroundColor = 'window';
field.style.color = 'windowtext';
}
function validateAddress(incoming) {
var emailstring = incoming;
var ampIndex = emailstring.indexOf("@");
var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
// find a dot in the portion of the string after the ampersand only
var dotIndex = afterAmp.indexOf(".");
// determine dot position in entire string (not just after amp portion)
dotIndex = dotIndex + ampIndex + 1;
// afterAmp will be portion of string from ampersand to dot
afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
// afterDot will be portion of string from dot to end of string
var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
var beforeAmp = emailstring.substring(0,(ampIndex));
var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
// index of -1 means "not found"
if ((emailstring.indexOf("@") != "-1") &&
(emailstring.length > 5) &&
(afterAmp.length > 0) &&
(beforeAmp.length > 1) &&
(afterDot.length > 1) &&
(email_regex.test(emailstring)) ) {
return true;
} else {
return false;
}
}
function isNumeric(sText) {
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;

for (var i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;

}
otiller is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-23-2008, 02:37 AM Re: Javascript works in firefox but not in IE explorer
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I'll tell you what. If you'll use code tags, I'll look at the code and see if I can assist.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-23-2008, 02:58 AM Re: Javascript works in firefox but not in IE explorer
Novice Talker

Posts: 14
Name: Orion Tiller
Trades: 0
Better?

HTML Code:
<script language="javascript" type="text/javascript" src="validate.js"></script>
<form name="FormName" method="post" action="index.php" onsubmit="return validate(this);">
PHP Code:
function validate(form) {
    var 
valid true;
    var 
FirstErrorField '';
    var 
checkboxes = new Array();
    var 
thisbox '';
    
    for(
i in form.elements ) {
        
//alert( i );

        
try{
            if( (
form.elements[i].type == "checkbox") && (checkboxes[form.elements[i].getAttribute("name")] == undefined) ) {
                
thisbox form.elements[i].getAttribute("name");
                
checkboxes[thisbox] = false;
                for(
j in form.elements) {
                    if( 
form.elements[j].type == "checkbox" ) {
                        if (
form.elements[j].getAttribute("name") == thisbox ) {
                            
normalField(form.elements[j].parentNode);
                            
checkboxes[thisbox] = (checkboxes[thisbox] || (form.elements[j].checked));
                        }
                    }
                }
                            
                if ( 
checkboxes[thisbox] == false ) {
                    for(
j in form.elements) {
                        if( 
form.elements[j].type == "checkbox" ) {
                            if (
form.elements[j].getAttribute("name") == thisbox ) {
                                
highlightField(form.elements[j].parentNode);
                            }
                        }
                    }
                    
valid false;
                }
            }
            else if(
form.elements[i].type != "hidden") {
                
//if(form.elements[i].parentNode.parentNode.parentNode.getElementsByTagName("div")[1].getElementsByTagName("label")[0].getAttribute("class") == "required") {
                
if(form.elements[i].getAttribute("class") == "required") {
                    if(
form.elements[i].value == '') {
                        
highlightField(form.elements[i]);
                        
valid false;
                    } 
                    else if( 
form.elements[i].getAttribute("title") == "numeric" ) {
                        if ( (
isNumeric(form.elements[i].value) == false) || ( form.elements[i].value.length form.elements[i].getAttribute("maxLength") ) ) {
                            
highlightField(form.elements[i]);
                            
valid false;
                        }
                        else {
                            
normalField(form.elements[i]);
                        }
                    }
                    else if( 
form.elements[i].getAttribute("name") == "EMAIL" ) {
                        if ( 
validateAddress(form.elements[i].value) == false ) {
                            
highlightField(form.elements[i]);
                            
valid false;
                        }
                        else {
                            
normalField(form.elements[i]);
                        }
                    }
                    else {
                        
normalField(form.elements[i]);
                    }
                }
            }
        } 
        catch( 
error ) {
        }
    }
    
    if( 
valid == false ) {
        
alert"Please fill in all required form fields with valid data." );
    } 
    return 
valid;
}

function 
highlightField(field) {
        
field.style.backgroundColor '#FF6666';
        
field.style.color 'white';
}

function 
normalField(field) {
        
field.style.backgroundColor 'window';
        
field.style.color 'windowtext';
}

function 
validateAddress(incoming) {
    var 
emailstring incoming;
    var 
ampIndex emailstring.indexOf("@");
    var 
afterAmp emailstring.substring((ampIndex 1), emailstring.length);
        
// find a dot in the portion of the string after the ampersand only
    
var dotIndex afterAmp.indexOf(".");
        
// determine dot position in entire string (not just after amp portion)
    
dotIndex dotIndex ampIndex 1;
        
// afterAmp will be portion of string from ampersand to dot
    
afterAmp emailstring.substring((ampIndex 1), dotIndex);
        
// afterDot will be portion of string from dot to end of string
    
var afterDot emailstring.substring((dotIndex 1), emailstring.length);
    var 
beforeAmp emailstring.substring(0,(ampIndex));
    var 
email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
        // index of -1 means "not found"
    
if ((emailstring.indexOf("@") != "-1") &&
        (
emailstring.length 5) &&
        (
afterAmp.length 0) &&
        (
beforeAmp.length 1) &&
        (
afterDot.length 1) &&
        (
email_regex.test(emailstring)) ) {
          return 
true;
    } else {
        return 
false;
    }
}    

function 
isNumeric(sText) {
   var 
ValidChars "0123456789.";
   var 
IsNumber=true;
   var 
Char;
 
   for (var 
0sText.length && IsNumber == truei++) 
      { 
      
Char sText.charAt(i); 
      if (
ValidChars.indexOf(Char) == -1
         {
         
IsNumber false;
         }
      }
   return 
IsNumber;
   
 } 
otiller is offline
Reply With Quote
View Public Profile
 
Old 02-23-2008, 03:12 AM Re: Javascript works in firefox but not in IE explorer
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I need the full HTML form (not the surrounding stuff). Then, I can create the files locally and see which error is thrown by IE when it's called as it didn't throw one when the code was loaded into a page. Also, I assume you have IE 7 running and not IE 6 as I upgraded and don't have IE 6 anymore. If that's not the case, let us know and perhaps someone else who maintains both versions can assist.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-23-2008, 03:14 AM Re: Javascript works in firefox but not in IE explorer
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
I'm not sure if it matters, Jeremy. I can see at least one potential flaw:
Code:
for(i in form.elements ) {


Change that to:
Code:
for (var i = 0; i < form.elements.length; i++) {


Anywhere else you see a for loop like that, try that.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 02-23-2008, 03:16 AM Re: Javascript works in firefox but not in IE explorer
Novice Talker

Posts: 14
Name: Orion Tiller
Trades: 0
It's IE 7 and you can see the code at
http://www.asapdegree.com

will that suffice?
otiller is offline
Reply With Quote
View Public Profile
 
Old 02-23-2008, 03:22 AM Re: Javascript works in firefox but not in IE explorer
Novice Talker

Posts: 14
Name: Orion Tiller
Trades: 0
I think that did the trick Adam. Thanks so much!!
otiller is offline
Reply With Quote
View Public Profile
 
Old 02-23-2008, 12:56 PM Re: Javascript works in firefox but not in IE explorer
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
No probs, dude. Good luck to you!
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 02-23-2008, 05:54 PM Re: Javascript works in firefox but not in IE explorer
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
for(i in form.elements )
Man...I never even checked that.
Firefox implement an enumerator on forms element natively !?
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Javascript works in firefox but not in IE explorer
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29507 seconds with 12 queries