PDA

View Full Version : simple function help


goozer
03-27-2003, 03:16 PM
I need a function that will add the numeric value of 3 form fields and evaluate the total on form submit. If the total is less than 25, then an alert box opens and informs the user to make changes and then resubmit.

Thanks from a javascript newbie.

liorean
03-27-2003, 03:33 PM
On your form element:

onsubmit="return checkForm(this);"

In your script:

function checkForm(form){
var
result,
values=[
parseFloat(form.elements[[string NameOfFirstField]]),
parseFloat(form.elements[[string NameOfSecondField]]),
parseFloat(form.elements[[string NameOfThirdField]])
];
result=(isNaN(values[0])||isNaN(values[1])||isNaN(values[2])||(values[0]+values[1]+values[2]<25));
if(result)
alert('Fields doesn't add up to 25 or higher. Please change your values and submit again.');
return !result;
}

goozer
03-27-2003, 04:15 PM
Hej,

Tack for the quick reply, however I'm having a problem getting this to work. When I test, the form submits. Here is the code on my page:

<script language="JavaScript" type="text/JavaScript">
function checkForm(form){
var
result,
values=[
parseFloat(form.elements[[engqty]]),
parseFloat(form.elements[[spanqty]]),
parseFloat(form.elements[[u21qty]])
];
result=(isNaN(values[0])||isNaN(values[1])||isNaN(
values[2])||(values[0]+values[1]+values[2]<25));
if(result)
alert("Fields don't add up to 25 or higher. Please change your values.");
return !result;
}

</script>

<input type="submit" name="Submit" value="Submit" onSubmit="return checkForm(this);">

What am I doing wrong?

HairyTeeth
03-27-2003, 10:45 PM
Not answering your specific question but, maybe this will help....



<html>
<head>
<title>Form Addition Test</title>

<script language="javascript" type="text/javascript">
<!-- ;

function checkResult(form) {
var fld_01 = parseFloat(form.fld_01.value)
var fld_02 = parseFloat(form.fld_02.value)
var fld_03 = parseFloat(form.fld_03.value)
var total = (fld_01 + fld_02 + fld_03)

if(total <= 24){
alert("The total you have entered is: '" + total + "'. \rPlease make the appropriate changes and then resubmit")
return false;
} else if(isNaN(total)) {
alert("Please check that you have entered numbers in all the fields")
return false;
} else {
alert("The minimum total of 25 has been exceeded. \rYour total is: '" + total+"'")
return true;
}
}
// end hide -->
</script>

</head>

<body>

<h1>Form Addition Test</h1>

<form action="javascript:alert('Success')" method="post" name="frm01" onsubmit="return checkResult(this)">
<input type="text" name="fld_01" size="10" maxlength="4"/>
<br />
<input type="text" name="fld_02" size="10" maxlength="4" />
<br />
<input type="text" name="fld_03" size="10" maxlength="4" />
<br />
<input type="submit" value="Test" />
</form>

</body>
</html>

liorean
03-27-2003, 11:53 PM
Originally posted by goozer
Hej,

Tack for the quick reply, however I'm having a problem getting this to work. When I test, the form submits. Here is the code on my page:

<script language="JavaScript" type="text/JavaScript">
function checkForm(form){
var
result,
values=[

parseFloat(form.elements[[engqty]]),
parseFloat(form.elements[[spanqty]]),
parseFloat(form.elements[[u21qty]])
];
result=(isNaN(values[0])||isNaN(values[1])||isNaN(
values[2])||(values[0]+values[1]+values[2]<25));
if(result)
alert("Fields don't add up to 25 or higher. Please change your values.");
return !result;
}

</script>

<input type="submit" name="Submit" value="Submit" onSubmit="return checkForm(this);">

What am I doing wrong?
Tjenixen! ;-)

First of all, edit the values variable:

values=[
parseFloat(form.elements['engqty']),
parseFloat(form.elements['spanqty']),
parseFloat(form.elements['u21qty'])
];


Second, onsubmit goes on the form tag, not the submit button.

Third, I just realised I made a mistake there. Correct the values variable to this instead:

values=[
parseFloat(form.elements['engqty'].value),
parseFloat(form.elements['spanqty'].value),
parseFloat(form.elements['u21qty'].value)
];


Ha en artun da'! som en Jamte skulle ha sagt.

goozer
03-28-2003, 01:16 PM
Thanks and Tack to both of you..it works just as I needed it to. I really appreciate your help! :thumbsup: And, by the way, the only reason I know the smallest amount of Swedish is that we have a satellite office in Stockholm!