Alright, I think 3 hours of feeling like an idiot and searching and then feeling even more stumped is grounds enough to post here.

I am basically just practicing with javascript (Just started a few days ago) and wanted to create an array that pretty much checked a form for errors, listed errors, then spewed them out in the end.

I am sure there is source code for something like this, but I wanted to just do this out of practice, as I have done this before in PHP.

I am basically making a function with a bunch of if statements and if any of them pop up I try to PUSH a string into the array. Now, it has worked, somewhat and sometimes. But then it just plain stopped working.

I played with the code for a while, placing alerts in parts of the function and found that it WAS the push code that stopped it from running completely. Can anyone help? I honestly don't think posting my code will help. Can a Push not exist in an If statement?

[Yes, I have tried to old LENGTH fashion way of doing it. NO my software is not outdated]

I'm no expert but I don't see why push can't be done based on the result of a condition such as an if statement. Here's an example:

<html>
<body>

<script type="text/javascript">

var arr = new Array(0);
var s = "";

while (s != "quit")
   {
    s = prompt("Enter a word", "quit");
    if (s != "don't push")
    {
      arr.push(s);
    }
    alert("Array now contains: " + arr);
   }
</script>

</body>
</html>

Well the code isn't quite like that. It's in an if statement that checks a field in a form, if it comes up false, then it inserts a PUSH, then goes to the next if. And at the end if the array > 0, then it's supposed to spit out the array as a string.

But the fact is that the code stops immediately at the push instance.

Well the code isn't quite like that. It's in an if statement that checks a field in a form, if it comes up false, then it inserts a PUSH, then goes to the next if. And at the end if the array > 0, then it's supposed to spit out the array as a string.

I added push instances to the example found at http://www.w3schools.com/JS/tryit.asp?filename=tryjs_formvalidate and tested it. It seems to work OK. If your code is too complex or context-dependent to post here, can you invent a simplified example that demonstrates the problem?

<html>
<head>
<script type="text/javascript">
function validate()
{
var at=document.getElementById("email").value.indexOf("@");
var age=document.getElementById("age").value;
var fname=document.getElementById("fname").value;
var arr = new Array(0);
var msg = "";
submitOK="true";

if (fname.length>10)
 {
 msg = "The name may have no more than 10 characters";
 alert(msg);
 arr.push(msg);
 submitOK="false";
 }
if (isNaN(age)||age<1||age>100)
 {
 msg = "The age must be a number between 1 and 100";
 alert(msg);
 arr.push(msg);
 submitOK="false";
 }
if (at==-1) 
 {
 msg = "Not a valid e-mail!";
 alert(msg);
 arr.push(msg);
 submitOK="false";
 }
if (submitOK=="false")
 {
 alert("The following errors occurred: " + arr); 
 return false;
 }
}
</script>
</head>

<body>
<form action="tryjs_submitpage.htm" onsubmit="return validate()">
Name (max 10 characters): <input type="text" id="fname" size="20"><br />
Age (from 1 to 100): <input type="text" id="age" size="20"><br />
E-mail: <input type="text" id="email" size="20"><br />
<br />
<input type="submit" value="Submit"> 
</form>
</body>

</html>

Mosel,

Nothing should prevent array.push() from working within an if clause, as long as array is in scope and whatever you are trying to push is valid.

Scope is different in javascript from php. Undeclared variables in functions are GLOBAL. Declared variables are LOCAL.

I guess that your problem is something other than push(). The code is probably hitting an error, maybe a scope thing or maybe it's trying to address a non-existant DOM element. Check your javascript error console (or IE error alert).

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.