Forum Moderators: open

Message Too Old, No Replies

Dual validation

validation issue

         

wwiii

2:29 am on Jun 16, 2009 (gmt 0)

10+ Year Member



Hi
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.

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

this is as close as i have come to getting it to work so far


<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 (!validate2()) { //validate the second way
alert("OOPS! Cakes MUST Equal 12")
return false;
}
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 validate2(){
x=document.BitsCrumblesMashForm
txt=x.pkgsTotal.value
if (txt!=12) {
return true
}else{
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>

Jesdisciple

1:54 pm on Jun 16, 2009 (gmt 0)

10+ Year Member



Could you explain what the first validation function is doing?

Also, the 'txt' name for both variables is confusing; it looks like your functions check for opposite conditions.

[edited by: Jesdisciple at 1:55 pm (utc) on June 16, 2009]

wwiii

3:04 pm on Jun 16, 2009 (gmt 0)

10+ Year Member



i can try but it may be ezr for u to view the rough draft of the form @ http:// www.aviancuisine. com/index.php?main_page=page_3

the first function (calc) does 2 things

1) it adds the qty boxes left to right and multiplies the totals by a set price placing the results in the "Pkgs" and "Price" boxes respectively.

2) it adds the pkgs and price boxes top to bottom and puts totals in appropriate total box

2nd function (validation - this is where problems begin)

is supposed to make sure none of the qty boxes is >11

3rd function (the 2nd validation)

is supposed to make sure that the pkgsTotal box = 12

so i need to validate that ALL qty boxes with the exception of pkgsTotal are < 12 and the pkgsTotal box MUST = 12

hope that helps

thx for ur time

Jesdisciple

6:24 pm on Jun 16, 2009 (gmt 0)

10+ Year Member



Alright, I get it now that I've seen the page.

I hope you have a very detailed explanation for your users of why things work the way they do, and you might need to rewrite these errors so they don't seem contradictory:

alert('OOPS! You have chosen 12 of something!');
alert('OOPS! Cakes MUST Equal 12');

And Firebug says your functions aren't even executing when I set breakpoins all over it. I think it's because you have an <html> element inside a <div>... Did you know it was that way? EDIT: Never mind about the breakpoints, but why is your HTML like that?

Also, try getting rid of these Firefox errors, or at least the JavaScript ones. I wonder if maybe they are the problem (when the HTML works) instead of your validation.

Warning: Error in parsing value for property 'font'. Declaration dropped.
Source File: http://www.aviancuisine.com/includes/templates/my_template/css/stylesheet.css
Line: 931

Error: syntax error
Source File: http://www.aviancuisine.com/includes/templates/my_template/jscript/jscript_statcounter.js
Line: 7
Source Code:
</script>

Error: XML tag name mismatch (expected img)
Source File: http://www.aviancuisine.com/includes/templates/my_template/jscript/jscript_statcounterb.js
Line: 7, Column: 29
Source Code:
alt="counter for blogger" ></a></div></noscript>

Error: The stylesheet http://www.aviancuisine.com/form_styles.css was not loaded because its MIME type, "text/html", is not "text/css".
Source File: http://www.aviancuisine.com/index.php?main_page=page_3
Line: 0

Error: googleanalytics_outgoing_init is not defined
Source File: http://www.aviancuisine.com/index.php?main_page=page_3
Line: 1

[edited by: Jesdisciple at 6:28 pm (utc) on June 16, 2009]

wwiii

6:54 pm on Jun 16, 2009 (gmt 0)

10+ Year Member



yes there will be an explanation written for the form (i can give u detailed reason for doing it this way if u'd like but not really relevant)and better error mssgs. those were quick and ez for testing purposes.

html thing was accidental from the way i copy and pasted, working copy is not like that. my bad for not checking closer b4 i posted.

like i said i am not a coder by any stretch so i really don't know the debugging process u went thru. i have firebug but i don't have a clue how to use it. :-D

i can tell u that a few of the errors i knew of (the css)just have not fixed but i did not know of the google and jscounter errors. i will fix thanks for that info.

anyway i got a solution to my problem from another forum. i will post the resulting code so maybe it can help some else.

thx much for ur time and effort. i do appreciate it

here is the solution:


function validate()
{
var under12=['bbbits','bbcrumbles','bbmash','bsbits','bscrumbles','bsmash','ffbits','ffcrumbles','ffmash','ppbits','ppcrumbles','ppmash','ssbits','sscrumbles','ssmash']

var isValid=true
for (var i=0;i<under12.length;i++) if (parseInt(document.BitsCrumblesMashForm[under12[i]].value,10)>=12) isValid=false

if (!isValid) alert('OOPS! You have chosen 12 of something!');

if (parseInt(document.BitsCrumblesMashForm.pkgsTotal.value,10)!=12) {
alert('OOPS! Cakes MUST Equal 12');
isValid=false;
}

return isValid;
}

Jesdisciple

7:08 pm on Jun 16, 2009 (gmt 0)

10+ Year Member



Since it's working, there's no need for you to respond again although you can if you'd like.

To use Firebug, click the little orange bug in the bottom-right of Firefox. To set breakpoints, click the Script tab; enable Script (and Console, Net) debugging for the site; open the dropdown just above the Script tab and select a file to debug; click a JS line to set a breakpoint. When the interpreter reaches that point, it will stop and let you examine variables. See also [getfirebug.com ]