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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation - A new way needed...

Status
Not open for further replies.

Sensibilium

Programmer
Apr 6, 2000
310
GB
I am currently developing a database-driven website, which will hopefully provide vistors with a list of all wedding lists currently on file. This is the simple bit, and basically solved.

My main problem is the form validation.

weddings.asp - Displays all wedding lists from the database, creating the link to each list as going through the loop.

weddinglist.asp - Displays all items on the selected wedding list.

Now, on the weddinglist.asp page, the Quantity Required is calculated from the Quantity Ordered less the Quantity Sold, and the user is given the option of entering the number of that product they wish to purchase with an input text box (there is one input text box per product item line).

After the user has entered all the quantities required they will then click on the 'Calculate' button, which takes the Quantity Required and multiplies it by the Retail Price, for each product item line, and adds them all together to give a total cost to the user.

I have written (and stolen and ripped, etc.) the Javascript to verify that the Quantity entered by the user is not more than the Quantity Available and to Calculate the total cost, the problem occurs when I test for cross-browser compatibility.

Currently, when the user enters the Required Quantity the value is checked when the field loses focus (using onBlur), and the Total Cost is calculated when the button has been clicked (using onClick). This doesn't work with NS4.7 and crashes NS6 under certain circumstances (only when a user enters an invalid Quantity value and immediately clicks the Calculate button).

So, to cut this post short, my hope is that someone could point me in the right direction for the browser problems I'm suffering. I'm thinking that probably the best way would be to check the Quantities entered by the user only after the Calculate button has been pressed, but I have no idea how to connvert my code to do this!

Any help would be greatly appreciated. Ahdkaw
"What would you expect from a bunch of monkeys?"
 
Here's the Javascript I'm currently using:

Code:
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!-- Begin
function checkNumeric(objName,minval, maxval,comma,period,hyphen) 
{
	var numberfield = objName;
	if (chkNumeric(objName,minval,maxval,comma,period,hyphen) == false) 
	{
		numberfield.select();
		numberfield.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function chkNumeric(objName,minval,maxval,comma,period,hyphen) 
{
  // only allow 0-9 be entered, plus any values passed 
  // (can be in any order, and don't have to be comma, period, or hyphen)
  // if all numbers allow commas, periods, hyphens or whatever, 
  // just hard code it here and take out the passed parameters
  var checkOK = &quot;0123456789&quot; + comma + period + hyphen;
  var checkStr = objName;
  var allValid = true;
  var decPoints = 0;
  var allNum = &quot;&quot;;

  for (i = 0;  i < checkStr.value.length;  i++)
  {
    ch = checkStr.value.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch != &quot;,&quot;)
      allNum += ch;
  }
  if (!allValid)
  {	
   alertsay = &quot;Please enter only numeric values in this field.&quot;
   alert(alertsay);
   return (false);
  }

  // set the minimum and maximum
  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != &quot;&quot; && !(prsVal >= minval && prsVal <= maxval))
  {
   alertsay = &quot;Please enter a value between &quot;
   alertsay = alertsay + &quot;\&quot;&quot; + minval + &quot;\&quot; and &quot;
   alertsay = alertsay + &quot;\&quot;&quot; + maxval + &quot;\&quot; in this field.&quot;
   alert(alertsay);
   return (false);
  }
}

function CalculateList(theForm)
{
	inputkind = 0;
    TotalValue = 0;
	for (i=0,n=theForm.elements.length;i<n;i++) {
	  inputkind = inputkind + 1;
	  if (inputkind == 1) {
		 Price = theForm.elements[i].value;
	  }
	  if (inputkind == 2) {
         if (theForm.elements[i].value != '') {
			LineValue = 0;
            LineValue = theForm.elements[i].value * Price;
			TotalValue = TotalValue + LineValue;
		 }
	  inputkind = 0;
      }
   }
   if (TotalValue != 0) {
      alert('Total cost to you is ' + FormatCurrency(TotalValue));
  }
  return true;
}

function FormatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = &quot;0&quot;;
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
pence = num%100;
num = Math.floor(num/100).toString();
if(pence<10)
pence = &quot;0&quot; + pence;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '£' + num + '.' + pence);
}
//  End -->
<!-- Begin
function LockField(myTextField,MaxQty) {
if (MaxQty == 0) {
	this.Blur();
	}
return true;
}
//  End -->
</script>

And here is the form creation VBScript code:

Code:
<%
  	If orsWeddingList.EOF Then
		bolNoGiftsOnList = True
  %>
                <tr valign=&quot;top&quot; align=&quot;center&quot;> 
                  <td colspan=5 width=550 height=40 valign=middle> 
                    <p class=&quot;list&quot;><b>Sorry, There are no items 
                      on this Wedding List just yet.</b></p>
                  </td>
                </tr>
                <%
	Else
		TotalLines = orsWeddingList.Recordcount
		Dim qty
		
		Dim Lines()
		ReDim Lines(TotalLines)
		ListItem = 1
  %>
                <tr> 
                  <td align=&quot;left&quot; width=55 bgcolor=&quot;#99CCCC&quot;> </td>
                  <td align=&quot;left&quot; width=330 bgcolor=&quot;#99CCCC&quot;><P><b> Product Name</b></td>
                  <td align=&quot;right&quot; width=55 bgcolor=&quot;#99CCCC&quot;><P><b>Price</b> </td>
                  <td align=&quot;right&quot; width=55 bgcolor=&quot;#99CCCC&quot;><P><b>Quantity</b> <br><b>Ordered</b> </td>
                  <td align=&quot;right&quot; width=55 bgcolor=&quot;#99CCCC&quot;><P><b>Qty Left</b> <br><b>To Buy</b> </td>
                </tr>
                <%
  	End If
	Do While Not orsWeddingList.EOF
		iListID = orsWeddingList(&quot;ListID&quot;)
		iQtyLeft = orsWeddingList(&quot;QtyOrdered&quot;) - orsWeddingList(&quot;QtyPurchased&quot;)
		sRetailPrice = &quot;£&quot; & FormatNumber(orsWeddingList(&quot;Price&quot;),2)
  %>
                <tr> 
                  <td height=25 valign=middle align=&quot;center&quot; width=55 bgcolor=&quot;#FFFFCC&quot;> 
                    <INPUT TYPE=&quot;hidden&quot; Name=&quot;Price&quot; Value=&quot;<%=orsWeddingList(&quot;Price&quot;)%>&quot;>
					<%
					If iQtyLeft = 0 Then
					%>
                    <input class=&quot;text&quot; type=&quot;text&quot; Name=&quot;Line<%=ListItem%>&quot; size=&quot;3&quot; maxlength=&quot;<%=Len(iQtyLeft)%>&quot; onFocus=&quot;this.blur()&quot; Value=&quot;0&quot;>
					<%
					Else
					%>
                    <input class=&quot;text&quot; type=&quot;text&quot; Name=&quot;Line<%=ListItem%>&quot; size=&quot;3&quot; maxlength=&quot;<%=Len(iQtyLeft)%>&quot; onLostFocus=&quot;checkNumeric(this,1,<%=iQtyLeft%>,'','','');return true;&quot;>
					<%
					End If
					%>
                  </td>
                  <td height=25 width=330 bgcolor=&quot;#FFFFCC&quot;> 
                    <p> <%=orsWeddingList(&quot;ProductName&quot;)%></p>
                  </td>
                  <td height=25 align=&quot;right&quot; width=55 bgcolor=&quot;#FFFFCC&quot;>
                    <p><%=sRetailPrice%> </p>
                  </td>
                  <td height=25 align=&quot;right&quot; width=55 bgcolor=&quot;#FFFFCC&quot;>
                    <p><%=orsWeddingList(&quot;QtyOrdered&quot;)%> </p>
                  </td>
                  <td height=25 align=&quot;right&quot; width=55 bgcolor=&quot;#FFFFCC&quot;>
                    <p><b><%=iQtyLeft%> </b></p>
                  </td>
                </tr>
                <%
		orsWeddingList.MoveNext
		ListItem = ListItem + 1
	Loop
	orsWeddingList.Close()
	If Not bolNoGiftsOnList Then
%>
                <tr> 
                  <td width=550 height=25 colspan=5 align=left valign=middle bgcolor=&quot;#99CCCC&quot;>  
                    <INPUT TYPE=&quot;button&quot; Name=&quot;Calculate&quot; Value=&quot;Calculate&quot; onClick=&quot;return CalculateList(this.form);&quot;>
                  </td>
                </tr>
<%
	End If
%>
Ahdkaw
&quot;What would you expect from a bunch of monkeys?&quot;
 
I am not going to say that I read either of your posts, because I havn't enough time, but from what I saw this might help you. have you thought about using regular expressions? check this out:


its a great refrence/tutorial. theEclipse
eclipse_web@hotmail.com
**\\||It was recently discovered that research causes cancer in rats||//**
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top