Problem for good specialist - variable changing by itself.

This is a discussion on "Problem for good specialist - variable changing by itself." within the JavaScript Forum section. This forum, and the thread "Problem for good specialist - variable changing by itself. are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Feb 27th, 2008, 15:10
New Member
Join Date: Feb 2008
Location: Belgium
Age: 26
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Problem for good specialist - variable changing by itself.

Hello Javascript specialists,

I'm trying to build a website with a register page, and validating the fields with javascript. This validation is being done every time you go out of an input element, as well as before the form is being submitted.

To do the verification I use a global variable "errors" which is false if there are no errors found, and true if there are errors found. It works great on Mozilla & Opera, but when I tested it on I.E. 6.0 (on windows XP) it behaved very strangly...

After a while I found out that the variable "errors" wasn't correct, and when I used alert(errors); , I've noticed that the value shown to me was false (-->no errors), but the behavior was good because it was true (--> errors, do not submit).

Then doing alert(errors); alert(errors); directly after each other first gave me false, and then true!?

To see what I'm talking about I made my code a lot shorter (containing the same problem), and attached that here:


Code: Select all
 <html>
<head>
<title>I hate IE</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--
var errors = false;

function required(HTMLInputElement, message) {
removeError(HTMLInputElement,message);
if(!HTMLInputElement.value)
createError(HTMLInputElement,message);
}

function createError(HTMLInputElement, errorMessage){
if(!errors)
errors = true;

var errorMessage = document.createTextNode(errorMessage);
var divObj = document.createElement("DIV");
divObj.className = "formErrorLine";
divObj.appendChild(errorMessage);

try{
divObj.attributes["id"] = "errorMessage";
}catch(e){}

try{
divObj.attributes["id"].value = "errorMessage";
}catch(e){}

HTMLInputElement.parentNode.parentNode.parentNode.insertBefore(divObj, HTMLInputElement.parentNode.parentNode.nextSibling);
}

function removeError(HTMLInputElement, errorMessage){
if(HTMLInputElement.parentNode.parentNode.nextSibling.nodeType == '1' && HTMLInputElement.parentNode.parentNode.nextSibling.firstChild.data == errorMessage){
HTMLInputElement.parentNode.parentNode.nextSibling.parentNode.removeChild(HTMLInputElement.parentNod e.parentNode.nextSibling);
}
}

function validateForm(){
errors = false;
validateHTMLInputElements();

/* normally, this would be the last line of this function:
return (!errors);
*/

//these alerts are here to show the problem:
alert("Are there any errors? -> " + errors);
alert("And now? -> " + errors);


return false;
}

function validateHTMLInputElements(){
var HTMLInputElements = document.getElementsByTagName("input");
for(i=0; i < HTMLInputElements.length; i++){
try {
var type = HTMLInputElements[i].attributes["type"].value;
HTMLInputElements[i].focus();
}catch(e){}
}
}
// -->
</SCRIPT>

<form name="register" method="post" onSubmit="return validateForm()" >
<div class='formLine'>
<div class='formLabelHeader'><label for="f_name">First name: </label></div>
<div class='formElementHeader'><input type="text" name="f_name" onblur="required(this,'please fill in your first name!'); "></div>
</div>
<div class='formLine'>
<div class='formElementHeader'><input type="submit" class="button" value="REGISTER" ></div>
</div>
</form>
</body>
</html>
To see the absurd behavior, just open the page, and before doing anything else, push the register button.

I think it might be an error in Jscript (the javascript engine of I.E.) and more specifically, something with their garbage collector, although I might be completely wrong.

If you've tried this, get the same error, and aren't using I.E. 6, please let me know what browser (and version) gave the same error.

If you've found a way to get the correct value into varable "errors" without having to use the alert() method, please tell me.

I really need some help with this one.
Reply With Quote

  #2 (permalink)  
Old Feb 29th, 2008, 16:46
New Member
Join Date: Feb 2008
Location: Boulder
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problem for good specialist - variable changing by itself.

Can you post a working example. the code above has missing parenthesis and misspelled parameters. I'm not even sure what the intent of this line is, so it's hard to help.
removeChild(HTMLInputElement.parentNod e.parentNode.nextSibling)
Reply With Quote
  #3 (permalink)  
Old Mar 11th, 2008, 12:28
New Member
Join Date: Feb 2008
Location: Belgium
Age: 26
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problem for good specialist - variable changing by itself.

It's okay, I've found my error. In IE the onblur event is triggered to late. So the solution is to not activate the code by using the onblur trigger, but instead activating it by directly executing the code, without activating the onBlur with focus() on every element
Reply With Quote
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing a session variable when clicking a link? Crow555 Classic ASP 0 May 15th, 2008 12:22
Variable problem trylah JavaScript Forum 0 Apr 11th, 2007 08:22
Another Variable problem VanderBOOM JavaScript Forum 2 Nov 16th, 2006 17:25
problem with Session variable andrew_perez5 Classic ASP 0 Jan 24th, 2005 03:42
Help changing the WHERE Variable? courtjester Databases 5 Jun 2nd, 2004 15:35


All times are GMT. The time now is 06:06.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43