|
-
Jun 5th, 2003, 11:35 AM
#1
Thread Starter
Fanatic Member
Error checking
Does anybody see why this script would not work?
Code:
<script language="JavaScript">
function calc(form)
{
var perc;
perc = Number(perc);
perc = 0;
var inputFields = document.all.tags("INPUT");
for(i=0; i < inputFields.length; i++)
{
if(inputFields[i].value != "")
{
if(inputFields[i].name != 'locID')
{}
if(inputFields[i].name != 'locCode')
{}
if(inputFields[i].name != 'org')
{}
if(inputFields[i].name != 'region')
{}
if(inputFields[i].name != 'percVal')
{}
if(inputFields[i].name != 'addIT')
{}
if(isNaN(inputFields[i].value) == true)
{
alert("You have input something other than a number, please input numbers only!");
return false;
}
else
{
var num;
alert("HI");
num = inpurFields[i].value;
num = Number(num);
perc = perc + num;
}
}
}
if(perc < 100)
{
alert("The percentages are less than 100, please check your numbers and re-submit")
return false;
}
else if(perc > 100)
{
alert("The percentages are more than 100, please check your numbers and re-submit")
return false;
}
}
</script>
-
Jun 6th, 2003, 06:09 AM
#2
In what way does it not work?
var perc;
perc = Number(perc);
perc = 0;
is nonsense.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 6th, 2003, 09:28 AM
#3
Thread Starter
Fanatic Member
It does nothing at all. Why is that syntax nonsense?
-
Jun 6th, 2003, 12:49 PM
#4
Addicted Member
Re: Error checking
Code:
<script language="JavaScript">
Function calc(form) {
var perc;
perc = Number(perc);
perc = 0;
var inputFields = document.all.tags("INPUT");
for(i=0; i < inputFields.length; i++) {
if(inputFields[i].value != "") {
if(isNaN(inputFields[i].value) == true) {
alert("You have input something other than a number, please input numbers only!");
return false;
}
Else {
var num;
alert("HI");
num = inpurFields[i].value;
num = Number(num);
perc = perc + num;
}
}
}
if(perc < 100) {
alert ("The percentages are less than 100, please check your numbers and re-submit")
return false;
}
else if(perc > 100) {
alert ("The percentages are more than 100, please check your numbers and re-submit")
return false;
}
}
</script>
This code makes little sense. I highlighted a typo I found.
But it will step through each member of the inputFields array. For each inputField, it will throw an alert is the value is not a number. If the value is a number, it will add it to perc. I think JavaScript supports the =+ operator, by the way. Double check the documentation.
If perc is less than or greater than 100, then you get another alert after it has stepped through each member of inputField. Since perc is a local variable, it dies when the function ends.
There were a lot of if clauses that did absolutely nothing, so I deleted them.
So, what is this code not doing?
Travis, Kung Foo Journeyman
Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
OSS: Mozilla, MySQL (Manual)
-
Jun 8th, 2003, 05:55 AM
#5
Originally posted by brianh
It does nothing at all. Why is that syntax nonsense?
1) It does something on three lines that would be more meaningful and easier to understand on one line.
2) The middle line is completly redundant. It really does nothing. You set a variable to the Number equivalent of its uninitialized (random) value. On the next line you set it to a number, changing the type to Number anyway.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|