My form has three text boxes named t1, t2 and t3. I created an array to loop through the boxes to make sure they have either a numeric entry or a zero. The function worked great while I had one generic alert. However, I would like to make the alert be more descriptive of the box. So, I created three alert messages named, msg1, msg2 and msg3. I figured as I looped through, this:
alert(msg+i)
would become:
alert(msg1), alert(msg2), alert(msg3), but it doesn't. I think it might be trying to mathematically add msg and the value for i. The full function is below. Any ideas?
function validateBox(){
var msg1="Please enter a valid amount in box 1."
var msg2="Please enter a valid amount in box 2."
var msg3="Please enter a valid amount in box 3."
for(i=1;i<4;i++){
if((!Number(document.getElementById("t"+i).value)) && (document.getElementById("t"+i).value!="0")){
alert(msg+i);
document.getElementById("t"+i).value="";
document.getElementById("t"+i).focus();
document.getElementById("t"+i).style.backgroundColor="yellow";
return false;}
}
}
Basically, all variables declared outside of a function (at least, if they are done so in the HEAD section of the document) become global variables -- which are stored as properties of the window object. It is as simple as that.
Bookmarks