The global variable that wasn't

2

2L

Trying to write some code and this is just shitting me now!
Bit of form validation stuff....


Variable is declared as follows.....

--snip--

<script language="JavaScript">
<!---
var errorList = 'Issues with the form are...\n';

--snip--

however onSubmit it loops thru a pile of functions doing all the validation
and is meant to be using

errorList =+ 'Error: ' + inputValue + ' must be a number\n'
errorList =+ 'Error: ' + inputValue.value + ' must be less than ' +
maxLength + ' characters long\n';
errorList =+ 'Error: ' + fieldName + ' cannot be empty\n';

etc from within those functions to add to the errors which then gets alerted
at the end of the originally called function and returns false if anything
has been added, however its just debugging the original value, but if I
place an alert after any of the above lines it'll give me the same thing,
not the adjusted string.

Thoughts?

TIA
 
S

Sean Jorden

var errorList = 'Issues with the form are...\n';

--snip--

however onSubmit it loops thru a pile of functions doing all the
validation and is meant to be using

errorList =+ 'Error: ' + inputValue + ' must be a number\n'
errorList =+ 'Error: ' + inputValue.value + ' must be less than ' +
maxLength + ' characters long\n';
errorList =+ 'Error: ' + fieldName + ' cannot be empty\n';


don't know if your actual code is like above but you have a syntax error..
should be += instead of =+
 
2

2L

don't know if your actual code is like above but you have a syntax error..
should be += instead of =+

Thats actually what I had originally, but thought I might have had a mental
blank so swapped it, even tried longhand with

errorList = errorList +

but still no luck.

Just downloaded latest version of Mozilla for its JS Debugger, but, IT WORKS
FINE IN IT!?!?

gah

Can I never win?!

(kinda getting annoyed with this one now!)

-_-
 
S

Sean Jorden

Thats actually what I had originally, but thought I might have had a
mental blank so swapped it, even tried longhand with

errorList = errorList +

but still no luck.

break it down until it works and then work backwards from there.


the following works by itself


var inputValue = '';
var fieldName = '';
var maxLength = 1;

var errorList = 'Issues with the form are...\n';

errorList += 'Error: ' + inputValue + ' must be a number\n';
errorList += 'Error: ' + inputValue.value + ' must be less than ' +
maxLength + ' characters long\n';
errorList += 'Error: ' + fieldName + ' cannot be empty\n';

alert(errorList);


some things to check..

- do inputValue, maxLength, and fieldName exist? if they are undefined,
javascript will choke and errorList will not get appended.

- any case issues?

- are you redefining errorList in a function call somehow, ie is it
really global?

put alert statements after each line just to make sure you are actually
appending the string at that particular point in time.
 
2

2L

Been doing some more investigating.....goddamn am I confused.

anwyays, heres a full code dump.....if it doesn't make sense to you, your
not the only one....

but trying to come up with something that works in all browser/os
combinations, however has to be easy to update/change what validation has to
be done without editing the JavaScript source (thus the onBlur functions)
etc.

ps...word wrap may well screw up some of the comments etc

www.wtf.net.au/temp/javascript.html

so there it is uploaded.

--

<html>

<head>

<script language="JavaScript">
<!---

// globals, might seem like these aren't needed, but they need to be passed
between multiple functions and without being passed by the function call

var submitted = '0';
var i = 0;
var elements;
var errorList;

function checkIsNum(inputValue,extras,min,max) {
if (submitted == "1") { // only execute on the form submit, not via
tabbing out etc
if (inputValue.length != 0) {
inputValue = inputValue.value;
var validChars = "0123456789" + extras;
var IsNumber;
var i;
IsNumber = true;
for (i = 0; i < inputValue.length && IsNumber == true; i++) {
checkCar = inputValue.charAt(i);
if (validChars.indexOf(checkCar) == -1) {
IsNumber = false;
errorList = errorList + 'Error: ' + inputValue + ' must be a number\n'
}
}
}
}
}

function checkNotNull(inputValue,fieldName) {
if (submitted == '1') { // only execute on the form submit, not via
tabbing out etc
valueToCheck = inputValue.value;
if (valueToCheck.length == 0) {
errorList = errorList + 'Error: ' + fieldName + ' cannot be empty\n';
}
}
}

function checkLength(toCheck,maxLength) {
if (submitted == '1') { // only execute on the form submit, not via
tabbing out etc
toCheck = document.form.number.value;
if (toCheck.length > maxLength) {
errorList = errorList + 'Error: ' + inputValue.value + ' must be less
than ' + maxLength + ' characters long\n';
}
}
}

// might seem like a stupid process, but I'm trying to make something where
the function can stay static and if you want to add a validation to a form
field, then you just add it on the onChange function, thusly when you submit
the form, it just loops thru the fields and gives them focus, then moves
onto the next object in effect making them execute their onBlur functions
(also explains the submitted variable).

// Issue seems to be that during the for (i = 0; i <= formElements; i++)
loop, it does everything OK, but it buffers the focus() commands until
after the function is finished, thus explaining why the variable doesn't get
added to....it does, just not until its too late!

function formSubmit() {
errorList = 'Issues with the form are...\n\n';
submitted = '1'; // tell the function that it needs to be processed
formElements = document.forms[0].length -1; // assume submit button is last
element, we don't need to check this
for (i = 0; i <= formElements; i++) { // loop thru the list of fields
eval('document.forms[0].elements[' + i + '].focus()'); // tell the item to
loose focus incase its got a check that needs to be executed
}
if (errorList == 'Issues with the form are...\n\n') {
submitted = '0'; // reset to 0 so it doesn't get executed on a non-form
submit
return true; // submit form, check was OK
} else {
alert (errorList); // show what the issues were
submitted = '0'; // reset to 0 so it doesn't get executed on a non-form
submit
return false; // die form, die!
}
}

//-->
</script>

</head>
<body>

<form name="form" method="post" onSubmit="return formSubmit();">

<input type="text" name="number" onBlur="checkNotNull(this,'Field 1');
checkIsNum(this,'./;'); checkLength(this,5)"><br>
<input type="text" name="number2" onBlur="checkNotNull(this,'Field 2');
checkIsNum(this,'./;'); checkLength(this,4)"><br>
<input type="text" name="number3" onBlur="checkNotNull(this,'Field 3');
checkIsNum(this,'./;'); checkLength(this,3)"><br>
<input type="text" name="number4" onBlur="checkNotNull(this,'Field 4');
checkIsNum(this,'./;'); checkLength(this,2)"><br>
<input type="submit" name="submit">


</form>
 
S

Steve van Dongen

Been doing some more investigating.....goddamn am I confused.

but trying to come up with something that works in all browser/os
combinations, however has to be easy to update/change what validation has to
be done without editing the JavaScript source (thus the onBlur functions)
etc.
// might seem like a stupid process, but I'm trying to make something where
the function can stay static and if you want to add a validation to a form
field, then you just add it on the onChange function, thusly when you submit
the form, it just loops thru the fields and gives them focus, then moves
onto the next object in effect making them execute their onBlur functions
(also explains the submitted variable).

// Issue seems to be that during the for (i = 0; i <= formElements; i++)
loop, it does everything OK, but it buffers the focus() commands until
after the function is finished, thus explaining why the variable doesn't get
added to....it does, just not until its too late!

Correct, onblur is executed asyncronously in IE5 and higher. There is
no way you'll be able to work around this and accomplish what you're
trying to do. Is there any particular reason you are trying to embed
all of the validation code inside the HTML elements?
function formSubmit() {
errorList = 'Issues with the form are...\n\n';
submitted = '1'; // tell the function that it needs to be processed
formElements = document.forms[0].length -1; // assume submit button is last
element, we don't need to check this
for (i = 0; i <= formElements; i++) { // loop thru the list of fields

By looping until i<=formElements you are including the submit button
which, according to your comment, wasn't your intention.
eval('document.forms[0].elements[' + i + '].focus()'); // tell the item to

I don't know where you learned how to do that with eval but unlearn
it.
document.forms[0].elements.focus();

Regards,
Steve
 
2

2L

Correct, onblur is executed asyncronously in IE5 and higher. There is
no way you'll be able to work around this and accomplish what you're
trying to do. Is there any particular reason you are trying to embed
all of the validation code inside the HTML elements?

--> insert profanities here <--

The reason....been trying to get something that I can just include a JS file
@ the top of any php/asp/cfml/whatever page where I need to do some form
validation and not have to worry about editing the JS in the headers, also
thought about passing an array in the onSubmit to tell it what to check,
however this means ensuring that the array is up to date. In my situation I
believe it is going to be much simpler if I can just add a text box, throw a
function onto the end of it with some vars and thats it. Might not make
sense to some people and there may be a better way, but thats my angle of
attack at the moment,.
function formSubmit() {
errorList = 'Issues with the form are...\n\n';
submitted = '1'; // tell the function that it needs to be processed
formElements = document.forms[0].length -1; // assume submit button is last
element, we don't need to check this
for (i = 0; i <= formElements; i++) { // loop thru the list of fields

By looping until i<=formElements you are including the submit button
which, according to your comment, wasn't your intention.

thus the
formElements = document.forms[0].length -1;
submit is always the last object on my forms
eval('document.forms[0].elements[' + i + '].focus()'); // tell the item
to

I don't know where you learned how to do that with eval but unlearn
it.
document.forms[0].elements.focus();


Consider it unlearnt!
Had some rather big headaches with JS before (self-taught) and was probably
something I learnt to do for another method and thus implemented it with
this cause that was just the way I did it.
Regards,
Steve

Cheers for that!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top