By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,705 Members | 2,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,705 IT Pros & Developers. It's quick & easy.

The global variable that wasn't

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"2L" <2L@--********@iinet.net.au> wrote in
news:av*******************@news-server.bigpond.net.au:
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 =+
--
In theory there is no difference between theory and practice. In practice
there is. - YB
Jul 20 '05 #2

P: n/a
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!)

-_-


Jul 20 '05 #3

P: n/a
In article <av*******************@news-server.bigpond.net.au>, "2L"
<2L@--********@iinet.net.au> writes:
<script language="JavaScript">
<script type="text/javascript">

language attribute is deprecated.
type attribute is mandatory in HTML4.0
<!---
the HTML comment is not needed.

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?


errorList += instead of errorList =+

var myVar = "This is myVar";

function myFunction(){
alert(myVar)
myVar += "\nPlus some more";
alert(myVar)
}
myFunction()

alerts This is myVar
followed by :
This is myVar
Plus some more
as would be expected.

If that doesn't resolve it, post back with either a URL, or more code.
--
Randy
All code posted is dependent upon the viewing browser
supporting the methods called, and Javascript being enabled.
Jul 20 '05 #4

P: n/a
"2L" <2L@--********@iinet.net.au> wrote in
news:mP*******************@news-server.bigpond.net.au:

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.


--
In theory there is no difference between theory and practice. In practice
there is. - YB
Jul 20 '05 #5

P: n/a
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>

---

Cheers.
Jul 20 '05 #6

P: n/a
On Mon, 25 Aug 2003 07:03:07 GMT, "2L" <2L@--********@iinet.net.au>
wrote:
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. <snip />
// 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[i].focus();

Regards,
Steve
Jul 20 '05 #7

P: n/a
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 lastelement, 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[i].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!
Jul 20 '05 #8

P: n/a
2L wrote:
Trying to write some code and this is just shitting me now!
Bit of form validation stuff....
<SNIP>
Thoughts?


Don't roll your own, use the freely available QForms API for form
validation, available at http://pengoworks.com/qforms/download/

Best I've ever seen by far.
Jul 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.