Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Non coder needs validation 1

Status
Not open for further replies.

wwiii12364

Technical User
Jun 12, 2009
10
US
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

Code:
<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="[URL unfurl="true"]http://www.aviancuisine.com/blog"[/URL]  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>
 
OOPS! Can't believe i left this info out.

pkgsTotal field MUST equal 12.
all other qty boxes MUST be less than 12.
 
Sup? My first thought would be that you have two functions called validate(). That does not sound good to me.

This might help
Code:
  function validate() {

...

        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>

Hope this helps.

"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
thx acent

that is much closer to working than it was but still not quite there.

that stopped posting even with error issue and it does seem to perform both validations.

however the problem is that if pkgsTotal = 12 i still get the "OOPS! You have chosen 12 of something!" error.

i really did not expect this to be so difficult.

thank u both for ur efforts
 
Hmmm...

Perhaps we have a problem where the function returns too soon.

Code:
  function validate() {

...

        var isValid = true;
        if (!validate2()) { //validate the second way
            alert("OOPS! Cakes MUST Equal 12");
            isValid = false;
        }
        if (txt < 12) {
            isValid = true;
        }else{
            alert("OOPS! You have chosen 12 of something!");
            isValid = false;
        }
        if(!isValid) 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>

By the way, I just noticed that most of your lines of code do not have a semicolon after them. You should be seeing scriting errors like there's no tomorrow.

Hope this helps.

"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
thx again acent

but that didn't work either.

with that change it still does both validations but if i hit submit button b4 pkgsTotal = 12 i get no error and form posts. if i fill qty's until pkgsTotal = 12 i get both error mssgs one rt after the other.

as far as semi-colons. i started this script with just a simple A+B=C script that i found on internet so i just emulated what i started with. think i read somewhere that jscript does not require semicolons.?? but i agree it is good practice. i don't get any kind of scripting errors.
 
You might try using 'parseInt' to convert the string into a number, otherwise testing for 'less than' might give unexpected results. Don't forget the second parameter to signify decimal.

You'll need this wherever you are setting 'txt':

Code:
txt = parseInt(x.pkgsTotal.value, 10);

Hope this helps,
Dan





Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
thx BillyRayPreachersSon,

i tried that and it did not change anything.

as i said i am not a programmer to make sure i did it rt here is what i did.

Code:
....
/* Checks Bits, Crumbles and MashQty Boxes - All Must be LESS than 12 */
function validate(){
        x=document.BitsCrumblesMashForm;
        txt=parseInt(x.bbbits.value, 10 || x.bbcrumbles.value, 10 || x.bbmash.value, 10 || x.bsbits.value, 10 || x.bscrumbles.value, 10 || x.bsmash.value, 10 || x.ffbits.value, 10 || x.ffcrumbles.value, 10 || x.ffmash.value, 10 || x.ppbits.value, 10 || x.ppcrumbles.value, 10 || x.ppmash.value, 10 || x.ssbits.value, 10 || x.sscrumbles.value, 10 || x.ssmash.value, 10);
         var isValid = true;
        if (!validate2()) { //validate the second way
            alert("OOPS! Cakes MUST Equal 12");
            isValid = false;
        }
        if (txt < 12) {
            isValid = true;
        }else{
            alert("OOPS! You have chosen 12 of something!");
            isValid = false;
        }
        if(!isValid) return false;
}
/* Checks sum of Total Packages Qty Box - Must equal 12 */
function validate2(){
        x=document.BitsCrumblesMashForm;
        txt = parseInt(x.pkgsTotal.value, 10);
        if (txt!= 12) {
            return true;
        }else{
            return false;
        }
}
 
I'd change this:

Code:
txt=parseInt(x.bbbits.value, 10 || x.bbcrumbles.value, 10 || x.bbmash.value, 10 || x.bsbits.value, 10 || x.bscrumbles.value, 10 || x.bsmash.value, 10 || x.ffbits.value, 10 || x.ffcrumbles.value, 10 || x.ffmash.value, 10 || x.ppbits.value, 10 || x.ppcrumbles.value, 10 || x.ppmash.value, 10 || x.ssbits.value, 10 || x.sscrumbles.value, 10 || x.ssmash.value, 10);

to this:

Code:
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;
txt = parseInt(txt, 10);



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
thx BillyRayPreachersSon

i made that change but made no diff.

i appreciate the efforts of everyone to help me out.

thank you!
 
btw if it would help at all u can view a rough draft of the form @ http:// www. aviancuisine. com/index.php?main_page=page_3
 
Hi

Here on Tek-Tips we used to thank for the received help by giving stars. Please click the

* [navy]Thank ***
for this valuable post![/navy]


at the bottom of ***'s post. That way you both show your gratitude and indicate this thread as helpful.

Feherke.
 
Feherke

i realize that and when my problem is solved i will do that.
 
Hi

Not sure if I understand you correctly, but I think you want something like this :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]validate[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] under12[teal]=[[/teal][green][i]'bbbits'[/i][/green][teal],[/teal][green][i]'bbcrumbles'[/i][/green][teal],[/teal][green][i]'bbmash'[/i][/green][teal],[/teal][green][i]'bsbits'[/i][/green][teal],[/teal][green][i]'bscrumbles'[/i][/green][teal],[/teal][green][i]'bsmash'[/i][/green][teal],[/teal][green][i]'ffbits'[/i][/green][teal],[/teal][green][i]'ffcrumbles'[/i][/green][teal],[/teal][green][i]'ffmash'[/i][/green][teal],[/teal][green][i]'ppbits'[/i][/green][teal],[/teal][green][i]'ppcrumbles'[/i][/green][teal],[/teal][green][i]'ppmash'[/i][/green][teal],[/teal][green][i]'ssbits'[/i][/green][teal],[/teal][green][i]'sscrumbles'[/i][/green][teal],[/teal][green][i]'ssmash'[/i][/green][teal]][/teal]

  [b]var[/b] isValid[teal]=[/teal][b]true[/b]
  [b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal];[/teal]i[teal]<[/teal]under12[teal].[/teal]length[teal];[/teal]i[teal]++)[/teal] [b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]document[teal].[/teal]BitsCrumblesMashForm[teal][[/teal]under12[teal][[/teal]i[teal]]].[/teal]value[teal],[/teal][purple]10[/purple][teal])>=[/teal][purple]12[/purple][teal])[/teal] isValid[teal]=[/teal][b]false[/b]

  [b]if[/b] [teal](![/teal]isValid[teal])[/teal] [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'OOPS! You have chosen 12 of something!'[/i][/green][teal]);[/teal]

  [b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]document[teal].[/teal]BitsCrumblesMashForm[teal].[/teal]pkgsTotal[teal].[/teal]value[teal],[/teal][purple]10[/purple][teal])!=[/teal][purple]12[/purple][teal])[/teal] [teal]{[/teal]
    [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'OOPS! Cakes MUST Equal 12'[/i][/green][teal]);[/teal]
    isValid[teal]=[/teal][b]false[/b][teal];[/teal]
  [teal]}[/teal]

  [b]return[/b] isValid[teal];[/teal]
[teal]}[/teal]
Note that such calculations can be easily faked, so I hope you are implementing a server-side validation too.

By the way, you know that you have two HTML documents there, one inside the other, right ?


Feherke.
 
PERFECT feherke!!!

Thank you very much

not worried about faking since it is just a request form no order is actually placed.

not sure what u mean bout the 2 html docs i don't see it.

again thank you very much for ur time
 
Hi

wwiii12364 said:
not sure what u mean bout the 2 html docs i don't see it.
Here are the structural elements together with the line numbers as appears in the browser's View Source command :
HTML:
[COLOR=white gray]   1 [/color] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
[COLOR=white gray]   2 [/color] <html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] dir="ltr" lang="en">
[COLOR=white gray]   3 [/color] <head>

[COLOR=white gray]  24 [/color] </head>
[COLOR=white gray]  25 [/color]
[COLOR=white gray]  26 [/color] <body id="page3Body" onload="googleanalytics_outgoing_init();">

[COLOR=white gray] 185 [/color] [red]<html>[/red]
[COLOR=white gray] 186 [/color] [red]<head>[/red]

[COLOR=white gray] 281 [/color] [red]<body>[/red]

[COLOR=white gray] 349 [/color] [red]</body>[/red]
[COLOR=white gray] 350 [/color] [red]</html>[/red]</div>

[COLOR=white gray] 525 [/color] </body></html>
Those in [red]red[/red] should not be there.


Feherke.
 
oh ok i c. that is just cuz of the way i copy and pasted it's not like that in real life.

thanks again to all of u for ur help i really appreciate it.
 
No problem. Just one suggestion, though:

In future, try using regular English instead of 'l33t txt spk' which 'u seem 2 b using, u c?'.

This is meant to be a forum for IT professionals, not 16-year old script kiddies.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top