PDA

View Full Version : validation help



wwiii
06-15-2009, 07:42 PM
1st thx 4 ur time. 2nd i am not a coder of any kind. i consider myself a code editor. i do pretty well editing various codes to suit my needs. this is my 1st attempt to edit jscript other than a basic variable change.

i have a form that i need to validate in 2 ways but i can only get the validations to work one or the other not both.

pkgsTotal field MUST equal 12.
all other qty boxes MUST be less than 12.

i am pretty sure i need to combine them with an OR(||)statement or something simple but i am at a loss as to how to do it. i have spent 2 days researching and trial and error to no avail.

if u could help me out i would greatly appreciate it.

again thx for your time





<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="form_styles.css" />

<script type="text/javascript">
function startCalc(){
interval = setInterval("calc()",1);
}
function calc(){
bbbits = document.BitsCrumblesMashForm.bbbits.value;
bbcrumbles = document.BitsCrumblesMashForm.bbcrumbles.value;
bbmash = document.BitsCrumblesMashForm.bbmash.value;
bblastBox = document.BitsCrumblesMashForm.bblastBox.value;

bsbits = document.BitsCrumblesMashForm.bsbits.value;
bscrumbles = document.BitsCrumblesMashForm.bscrumbles.value;
bsmash = document.BitsCrumblesMashForm.bsmash.value;
bslastBox = document.BitsCrumblesMashForm.bslastBox.value;

ffbits = document.BitsCrumblesMashForm.ffbits.value;
ffcrumbles = document.BitsCrumblesMashForm.ffcrumbles.value;
ffmash = document.BitsCrumblesMashForm.ffmash.value;
fflastBox = document.BitsCrumblesMashForm.fflastBox.value;

ppbits = document.BitsCrumblesMashForm.ppbits.value;
ppcrumbles = document.BitsCrumblesMashForm.ppcrumbles.value;
ppmash = document.BitsCrumblesMashForm.ppmash.value;
pplastBox = document.BitsCrumblesMashForm.pplastBox.value;

ssbits = document.BitsCrumblesMashForm.ssbits.value;
sscrumbles = document.BitsCrumblesMashForm.sscrumbles.value;
ssmash = document.BitsCrumblesMashForm.ssmash.value;
sslastBox = document.BitsCrumblesMashForm.sslastBox.value;
pkgsTotal = document.BitsCrumblesMashForm.pkgsTotal.value;

bbprice = document.BitsCrumblesMashForm.bbprice.value;
bsprice = document.BitsCrumblesMashForm.bsprice.value;
ffprice = document.BitsCrumblesMashForm.ffprice.value;
ppprice = document.BitsCrumblesMashForm.ppprice.value;
ssprice = document.BitsCrumblesMashForm.ssprice.value;

document.BitsCrumblesMashForm.bblastBox.value = (bbbits * 1) + (bbcrumbles * 1) + (bbmash * 1);
document.BitsCrumblesMashForm.bslastBox.value = (bsbits * 1) + (bscrumbles * 1) + (bsmash * 1);
document.BitsCrumblesMashForm.fflastBox.value = (ffbits * 1) + (ffcrumbles * 1) + (ffmash * 1);
document.BitsCrumblesMashForm.pplastBox.value = (ppbits * 1) + (ppcrumbles * 1) + (ppmash * 1);
document.BitsCrumblesMashForm.sslastBox.value = (ssbits * 1) + (sscrumbles * 1) + (ssmash * 1);
document.BitsCrumblesMashForm.pkgsTotal.value = (bblastBox * 1) + (bslastBox * 1) + (fflastBox * 1) + (pplastBox * 1) + (sslastBox * 1);

document.BitsCrumblesMashForm.bbprice.value = (bblastBox * 1) * (3.99 * 1);
document.BitsCrumblesMashForm.bsprice.value = (bslastBox * 1) * (3.99 * 1);
document.BitsCrumblesMashForm.ffprice.value = (fflastBox * 1) * (3.99 * 1);
document.BitsCrumblesMashForm.ppprice.value = (pplastBox * 1) * (2.99 * 1);
document.BitsCrumblesMashForm.ssprice.value = (sslastBox * 1) * (2.99 * 1);
document.BitsCrumblesMashForm.ttlprice.value = (bbprice * 1) + (bsprice * 1) + (ffprice * 1) + (ppprice * 1) + (ssprice * 1);
}
function stopCalc(){
clearInterval(interval);
}

/* Checks Bits, Crumbles and MashQty Boxes - All Must be LESS than 12 */
function validate(){
x=document.BitsCrumblesMashForm
txt=(x.bbbits.value || x.bbcrumbles.value || x.bbmash.value || x.bsbits.value || x.bscrumbles.value || x.bsmash.value || x.ffbits.value || x.ffcrumbles.value || x.ffmash.value || x.ppbits.value || x.ppcrumbles.value || x.ppmash.value || x.ssbits.value || x.sscrumbles.value || x.ssmash.value)
if (txt < 12) {
return true
}else{
alert("OOPS! You have chosen 12 of something!")
return false
}
}
/* Checks sum of Total Packages Qty Box - Must equal 12 */
function validate(){
x=document.BitsCrumblesMashForm
txt=x.pkgsTotal.value
if (txt!= 12) {
return true
}else{
alert("OOPS! Cakes MUST Equal 12")
return false
}
}
</script>




<title>basic autosum</title>
</head>

<body>




<form name="BitsCrumblesMashForm" action="http://www.aviancuisine.com/blog" onsubmit="return validate()" method="post">

<fieldset class="prod_selection">
<legend> Nutri-Bits, Crumbles and Mash Selections</legend> <br />
<div class="row"><span class="column_heading_bits">Bits</span> <span class="column_heading_crumbles">Crumbles</span><span class="column_heading_mash">Mash</span><span class="column_heading_pkgs">Pkgs</span><span class="column_heading_price">Price</span></div>
<div class="row"><span class="label2">Bug Buffet</span><span class="formw5">
<input class="right" type=text name="bbbits" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">+</span> <input class="right" type=text name="bbcrumbles" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">+</span> <input class="right" type=text name="bbmash" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">=</span> <input class="right" type=text name="bblastBox" readonly="readonly" size="1"/>
@ $3.99 ea = $
<input type=text name="bbprice" readonly="readonly" size="2"/>
</span></div>
<br />
<div class="row"><span class="label2">Broad Spectrum</span><span class="formw5">
<input class="right" type=text name="bsbits" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">+</span> <input class="right" type=text name="bscrumbles" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">+</span> <input class="right" type=text name="bsmash" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">=</span> <input class="right" type=text name="bslastBox" readonly="readonly" size="1"/>
@ $3.99 ea = $
<input type=text name="bsprice" readonly="readonly" size="2"/>
</span></div>
<br />
<div class="row"><span class="label2">Fruit Feast</span><span class="formw5">
<input class="right" type=text name="ffbits" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">+</span> <input class="right" type=text name="ffcrumbles" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">+</span> <input class="right" type=text name="ffmash" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">=</span> <input class="right" type=text name="fflastBox" readonly="readonly" size="1"/>
@ $3.99 ea = $
<input type=text name="ffprice" readonly="readonly" size="2"/>
</span></div>
<br />
<div class="row"><span class="label2">Peanut Paradise</span><span class="formw5">
<input class="right" type=text name="ppbits" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">+</span> <input class="right" type=text name="ppcrumbles" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">+</span> <input class="right" type=text name="ppmash" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">=</span> <input class="right" type=text name="pplastBox" readonly="readonly" size="1"/>
@ $2.99 ea = $
<input type=text name="ppprice" readonly="readonly" size="2"/>
</span></div>
<br />
<div class="row"><span class="label2">Seed Sensation</span><span class="formw5">
<input class="right" type=text name="ssbits" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">+</span> <input class="right" type=text name="sscrumbles" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">+</span> <input class="right" type=text name="ssmash" size="1" value="" onFocus="startCalc();" onBlur="stopCalc();"/>
<span class="symbol">=</span> <input class="right" type=text name="sslastBox" readonly="readonly" size="1"/>
@ $2.99 ea = $
<input type=text name="ssprice" readonly="readonly" size="2"/>
</span></div>


<br />
<div class="row"><span class="label2">Totals</span><span class="formw"> Pkgs
<input type=text name="pkgsTotal" readonly="readonly" size="1"/> Price
<input type=text name="ttlprice" readonly="readonly" size="2"/>
</span></div>

</fieldset>

<br />
<input type="submit" value="Submit" id="button" name="button" />
</form>

</body>
</html>

prasanthmj
06-16-2009, 03:12 AM
You can't have two functions with the same name. Create another function ValidateAll() that calls the other two functions.



function validateAll()
{
if(!validateQty())
{
return false;
}
if(!validatePkgsTotal())
{
return false;
}
return true;
}

/* Checks Bits, Crumbles and MashQty Boxes - All Must be LESS than 12 */
function validateQty(){
x=document.BitsCrumblesMashForm
txt=(x.bbbits.value || x.bbcrumbles.value || x.bbmash.value || x.bsbits.value || x.bscrumbles.value || x.bsmash.value || x.ffbits.value || x.ffcrumbles.value || x.ffmash.value || x.ppbits.value || x.ppcrumbles.value || x.ppmash.value || x.ssbits.value || x.sscrumbles.value || x.ssmash.value)
if (txt < 12) {
return true
}else{
alert("OOPS! You have chosen 12 of something!")
return false
}
}
/* Checks sum of Total Packages Qty Box - Must equal 12 */
function validatePkgsTotal(){
x=document.BitsCrumblesMashForm
txt=x.pkgsTotal.value
if (txt!= 12) {
return true
}else{
alert("OOPS! Cakes MUST Equal 12")
return false
}
}



Then update



<form name="BitsCrumblesMashForm" action="http://www.aviancuisine.com/blog" onsubmit="return validateAll()" method="post">


The following links might help
JavaScript Form Validation (http://www.javascript-coder.com/html-form/javascript-form-validation.phtml)

Form Validation (http://www.javascript-coder.com/html-form/form-validation.phtml)