473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

The global variable that wasn't

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
8 2194
"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
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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
5
by: sworna vidhya | last post by:
Hai, When viewing threads of comp.lang.c, I came across with 'static const char * const resultFileName = "param.txt";' . Here in this thread, 'static const char * const resultFileName =...
44
by: Mohanasundaram | last post by:
int i = 10; int main() { int i = 20; return 0; } Hi All, I want to access the global variable i inside the main. Is there
8
by: Steve Klett | last post by:
This is probably a commonly asked question, but I can't find a good way to search for it, so I will ask here. I assumed that there had to be an easy way to access a page level variable from a...
22
by: UJ | last post by:
How do I create an instance of a class that is accessible to all classes/forms in my project? I already have a startup module and I was thinking putting stuff in there and then accessing it through...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
16
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.