<form action="submit.php" name="entry" method="post" onsubmit="return validateEntry()">
<input type="text" name="Left1.6">
... many more input boxes Left1.1 to Left5.10 ...
Then I have my validation script like
if (x==null || x<9 || x>15)
var msg='It looks like you screwed up a value!\n';
msg = msg + 'Check out ';
msg = msg + batName + '!!';
Unfortunately, this script seems very hit-or-miss. Sometimes it catches errors and other times it doesn't. What's more is that even if it catches an error, it doesn't alert() me where the error is -- you can see I've tried to implement this by concatenating the problemed text box's name onto the alert() string.
Any ideas as for how to make my script more robust? I've been out of the coding game for quite a while now, and out of the JS game for longer. Please excuse my awful syntax.
Here's a suggested change. May or may not fit your needs exactly:
var form = document.getElementById("...id of your form...);
var msg = "";
var batName='Front' + shelf + '.' + bat;
var fld = forrm[batName];
var fld.style.backgroundColor = "transparent"; // assume valid
var x = Number( form[batName].value );
if ( fld.value == "" || isNaN(x) || x<9 || x>15)
fld.style.backgroundColor = "pink"; // indicate field in error
msg += batname + " is not a valid number between 9 and 15<br/>"
// clear or set message in place:
document.getElementById("putErrorMessageHere").innerHTML = msg;
// return true if no messages, else false
return ( msg == "" );
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 12-05-2012 at 08:26 PM..
First off, thanks for the replies. You are awesome.
isNaN looks like exactly what I was looking for. I should've guessed that, sorry.
I will try to drop your JS in when I have a moment. It *looks* like it's exactly what I want, though. Quick question: The "putErrorMessageHere" element can be anything? A <p> or a <div>, for example?
<p id="putErrorMessageHere"><!-- Text here will be populated by JS? --></p>
Alright! Yea -- turns out many of my errors were typos. As a matter of fact, a missed semicolon took the whole script from working like a dream to a nightmare. Anyway, everything seems to be working perfectly, now. Thanks so much for the help; It's been a long time since I put pen to Java.