IE7 Javascript Problem
-
I'm trying to do some form validation that involves looping through all elements in a form. It works perfectly in FireFox, but totally chokes in IE7. It seems to be that the $Form.elements.length value is not returning a number. I've written a little test function to simulate the error.
function validateForm() { alert(document.forms.length); var numElements = document.forms[0].elements.length; alert(numElements); }
First alert shows: 1 Second alert shows: [object] Anyone seen this happen before? Thanks a lot. :cool: -
I'm trying to do some form validation that involves looping through all elements in a form. It works perfectly in FireFox, but totally chokes in IE7. It seems to be that the $Form.elements.length value is not returning a number. I've written a little test function to simulate the error.
function validateForm() { alert(document.forms.length); var numElements = document.forms[0].elements.length; alert(numElements); }
First alert shows: 1 Second alert shows: [object] Anyone seen this happen before? Thanks a lot. :cool:try something like this
function checkForm()
{
var frm = document.getElementById('form1');
var strError = 'The following fields are required:\n';
var bValid = true;
var child;for(var i = 0; i < frm.childNodes.length; i++) { child = frm.childNodes.item(i); if(child.nodeType == 1) { if(child.tagName.toString().toLowerCase() == 'input') { if(child.type.toString().toLowerCase() == 'text') { if(child.value == '') { strError += '\\n'+ child.id.toString(); bValid = false; } } } } } if(!bValid) alert(strError); return bValid;
}
Works in both FF and IE7. It only checks input text's at the moment so you'll need to change it so that it's a bit more useful, but it should get you going. HTH
-
try something like this
function checkForm()
{
var frm = document.getElementById('form1');
var strError = 'The following fields are required:\n';
var bValid = true;
var child;for(var i = 0; i < frm.childNodes.length; i++) { child = frm.childNodes.item(i); if(child.nodeType == 1) { if(child.tagName.toString().toLowerCase() == 'input') { if(child.type.toString().toLowerCase() == 'text') { if(child.value == '') { strError += '\\n'+ child.id.toString(); bValid = false; } } } } } if(!bValid) alert(strError); return bValid;
}
Works in both FF and IE7. It only checks input text's at the moment so you'll need to change it so that it's a bit more useful, but it should get you going. HTH
Thanks very much for the reply, but I guess my main question here is why $Form.elements.length is not returning a number. This has always worked before....
-
Thanks very much for the reply, but I guess my main question here is why $Form.elements.length is not returning a number. This has always worked before....
Wierd, just done a tiny test myself using your code and get a number as you'd expect from elements.length Here is the code I ran:
function checkForm() { alert(document.forms.length); var numElements = document.forms[0].elements.length; alert(numElements); return true; } The first alert gives "1" and the second gives "5". If your form isn't too huge perhaps you could paste in the code that's giving you this wierd behaviour. HTH
-
Wierd, just done a tiny test myself using your code and get a number as you'd expect from elements.length Here is the code I ran:
function checkForm() { alert(document.forms.length); var numElements = document.forms[0].elements.length; alert(numElements); return true; } The first alert gives "1" and the second gives "5". If your form isn't too huge perhaps you could paste in the code that's giving you this wierd behaviour. HTH
Well, I feel dumb now. I had a textbox in the form named "length". :doh: Thanks for all your help.
-
Well, I feel dumb now. I had a textbox in the form named "length". :doh: Thanks for all your help.
hehe - one of those ;)