PDA

View Full Version : Function to check form data values


4ever
09-11-2002, 11:02 PM
Hello!
Could someone please look at a form page Iam working on. I am trying
to call a function named "rangeCheck()" to make shure that all the
values entered in all of the 8 textfields are between 1-5. If this is
true I need it to call the function calculate() to add all the text
fields together and put that value into a seperate text field called
"totalpoints". Could someone point me in the right direction, using the
general script I have started with? Here is the link to my
page.http://www.inahq.org/memberladder.html

Here is the part of the script in question....

var range = 0
var n = 0
var i = 0
var total = 0
function rangeCheck()
{
for (n=0; n<9; n++)
{
if (document.forms[0].elements[n].value > 5)
{
window.alert('Please enter a value between 1 and 5 for each points earned category.');
}
else
{
calculate();
}}

function calculate()
{
for (i=1, total=0 ; i<9; i++)
{
total += parsInt(document.forms[0].elements[i].value - 0);
document.memberladder.totalpoints.value = total;
}
}

martin_narg
09-12-2002, 07:36 AM
I think if i was doing this, i'd probably use select boxes (as users are only going to choose one of 5 options). However, this piece of scripting should work fine for your page.

Hope this helps

m_n


<html>
<head>
<title>form checker</title>
<script>
function checkMe() {
var isOK = true;
var count = 0;
for( var i =0; i<document.memberladder.elements.length; i++ ) {
var curVal = document.memberladder.elements[i];
if( curVal.name.indexOf('chk') == -1 )
continue;
if( isNaN(curVal.value) || curVal.value > 5 || curVal.value < 1 ) {
alert('Please enter a value between 1 and 5 for each points earned category. ['+curVal.name+']');
isOK = false;
break;
}
else
count += eval(curVal.value);
}
if( !isOK )
document.memberladder.totalpoints.value = '';
else
document.memberladder.totalpoints.value = count;
}
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="memberladder">
value 1: <input type="text" name="chk1"><br>
value 2: <input type="text" name="chk2"><br>
value 3: <input type="text" name="chk3"><br>
value 4: <input type="text" name="chk4"><br>
value 5: <input type="text" name="chk5"><br>
value 6: <input type="text" name="chk6"><br>
value 7: <input type="text" name="chk7"><br>
value 8: <input type="text" name="chk8"><br>
<input type="button" value="calculate" onClick="checkMe()"><br><br>
totalpoints: <input type="text" name="totalpoints">
</form>
</body>
</html>