Google Answers Logo
View Question
 
Q: JavaScript code needed for form handling ( No Answer,   3 Comments )
Question  
Subject: JavaScript code needed for form handling
Category: Computers > Programming
Asked by: herbet-ga
List Price: $15.00
Posted: 08 Apr 2003 00:19 PDT
Expires: 08 May 2003 00:19 PDT
Question ID: 187553
I have an HTML form with three input boxes; txtPrice, txtUnits and
txtTotal.
 txtPrice – is a dollar value, formatted to 4 decimal places (ie
$1,111.1111)
 txtUnits – is a numeric value formated to 4 decimal places (ie
1,111.1111)
 txtTotal – is a dollar value formatted to 2 decimal places (ie
$1,111.11)

I need to develop robust, browser independent code that will handle
the onChange event for each box to:
1. Check that text input is correct (simply blank the box and exit if
non-numeric data is entered)
2. Correctly format the data input
3. Calculate the value in the third box, given any two data inputs.
Where:
    txtTotal = txtPrice * txtUnits (rounded to 2 decimal points)
    txtPrice = txtTotal / txtUnits (rounded to 4 decimal points)
    txtUnits = txtTotal / txtPrice (rounded to 4 decimal points)

Code should be efficient, modulised, well documented and presented in
a single HTML document.

Clarification of Question by herbet-ga on 09 Apr 2003 07:08 PDT
SacGen, no attachments are necessary. Simply post your plain-text
answer as a complete HTML document, so that it can be simply
cut-and-paste to a text editor for testing. ie

<html>
<head>
 <title>test page</title>
</head>
<body>
 <!-- your answer here -->
</body>
</html>

Clarification of Question by herbet-ga on 18 Apr 2003 21:02 PDT
Sacgen this code works perfectly and I want to close the question.
However, if hammer is correct I don't think I can pay you through this
system. Frankly, this is silly as you have well earned your payment.
Are you able to register as a Google researcher?
Answer  
There is no answer at this time.

Comments  
Subject: Re: JavaScript code needed for form handling
From: sacgen-ga on 09 Apr 2003 06:35 PDT
 
Hi,

I am new to this question answer system!!

I have done the complete program as per your specifications and is
tested throughly. But I do not know how to post the answer to your
question on this system. If you know the way, please let me know!!

Regards,
SacGen
Subject: Re: JavaScript code needed for form handling
From: hammer-ga on 12 Apr 2003 06:03 PDT
 
sacgen-ga,

You can post your code as a comment. You don't get paid for this, but
you get to share your knowledge, and herbet-ga gets a free answer!
Only Researchers can post official answers and collect payment, but
Commenters can certainly "answer" questions in the comments area. The
part that is against the Terms of Service is posting your email
address and requesting direct contact.

- Hammer
Subject: Re: JavaScript code needed for form handling
From: sacgen-ga on 13 Apr 2003 22:18 PDT
 
Hi,

Here is the piece of code that I wrote earlier. Please let me know if
it works as you wished!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<HTML>
<HEAD>
<SCRIPT Language="JavaScript">

	function DoRequired (StrName,nValue)
	{
			//Variables
			var txtTextBox = new String(StrName);
			var tempString = new String(nValue);
						
			//Perform Numeric Check
			
			if(isNaN(nValue))
				{
					if (StrName.toString()=="txtPrice")             		
	             		document.forms(0).elements(0).value="";
	             	else if (StrName.toString()=="txtUnits")
	             		document.forms(0).elements(1).value="";
	             	else if (StrName.toString()=="txtTotal")
	             		document.forms(0).elements(2).value="";
	             	alert("Please enter Numeric value");
             	}	
			else 
             	switch (StrName)
             	{
             		case "txtPrice" :
 				           			formatValue(tempString, 4, 0);
										if(document.forms(0).elements(1).value != "")
										{
											nTotal =	document.forms(0).elements(0).value *
document.forms(0).elements(1).value;
											nTotal = Math.round(nTotal*100)/100;
											document.forms(0).elements(2).value = nTotal;
											tempString = new String(nTotal);
											formatValue(tempString, 2, 2)
										}
										else if(document.forms(0).elements(2).value != "")
										{
											nUnits = document.forms(0).elements(2).value/document.forms(0).elements(0).value;
											nUnits = Math.round(nUnits*10000)/10000;
											document.forms(0).elements(1).value = nUnits;
											tempString = new String(nUnits);
											formatValue(tempString, 4, 1)
										}			
             							break;
             			
	             	case "txtUnits" :
				             			formatValue(tempString, 4, 1);
				             			if(document.forms(0).elements(0).value != "")
										{
											nTotal =	document.forms(0).elements(0).value *
document.forms(0).elements(1).value;
											nTotal = Math.round(nTotal*100)/100;
											document.forms(0).elements(2).value = nTotal;
											tempString = new String(nTotal);
											formatValue(tempString, 2, 2)
										}
										else if(document.forms(0).elements(2).value != "")
										{
											nUnitPrice =
document.forms(0).elements(2).value/document.forms(0).elements(1).value;
											nUnitPrice= Math.round(nUnitPrice*10000)/10000;
											document.forms(0).elements(0).value = nUnitPrice;
											tempString = new String(nUnitPrice);
											formatValue(tempString, 4, 0)
										}
				             			break;
				             			
					case "txtTotal" :
										formatValue(tempString, 2, 2);
										if(document.forms(0).elements(0).value != "")
										{
											nUnits = document.forms(0).elements(2).value/document.forms(0).elements(0).value;
											nUnits = Math.round(nUnits*10000)/10000;
											document.forms(0).elements(1).value = nUnits;
											tempString = new String(nUnits);
											formatValue(tempString, 4, 1)
										}
										else if(document.forms(0).elements(1).value != "")
										{
											nUnitPrice =
document.forms(0).elements(2).value/document.forms(0).elements(1).value;
											nUnitPrice= Math.round(nUnitPrice*10000)/10000;
											document.forms(0).elements(0).value = nUnitPrice;
											tempString = new String(nUnitPrice);
											formatValue(tempString, 4, 0)
										}
										
				             			break;
             	}
             	
    }


//New function : This is reusable function which serves as a
independent serice responsible for formatting the data
//					The function accepts the value as entered by user, Number of
digits after the decimal point
// 					and the index of the elements as a paramenter. It determines
how many zeros are to be padded
//					in order to have the required number of digits after decimal
point

	function formatValue(tempString, nDecimalDigits, nElementIndex)
	{
		//Variables
		
		n=""; // variable to hold the decimal part of the entered value
		
		if(tempString.indexOf('.')!=-1) //If Decimal point exists then
       {
        	n=tempString.substr(tempString.indexOf('.')+1); //Extract the
decimal digits of the number
        	
        	if(n.length <= nDecimalDigits)
	        	{
	        		result = nDecimalDigits - n.length; //calculate how many
zeros are to be padded to the right
   		     		for(i=0; i<result; i++)
					{
						tempString = tempString + "0"; //pad the zeros to the right 
					}
					document.forms(0).elements(nElementIndex).value =
tempString;	//set the formatted string in the textbox
				}
			else
				{
					if (nElementIndex	==2)	
   						document.forms(0).elements(nElementIndex).value=
Math.round(document.forms(0).elements(nElementIndex).value * 100)/100;
   					else
   						document.forms(0).elements(nElementIndex).value=
Math.round(document.forms(0).elements(nElementIndex).value *
10000)/10000;
   				}
       }
       else
       {
       	if (nElementIndex!=2)
			   {document.forms(0).elements(nElementIndex).value=
document.forms(0).elements(nElementIndex).value + ".0000";}
			else
				{document.forms(0).elements(nElementIndex).value=
document.forms(0).elements(nElementIndex).value + ".00";}
       }
	}
	
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="GoogleAssign" ACTION="">
<BR>
<BR>
<TABLE BORDER = 0 width="447">
<TR>
<TD width="125"><B>&nbsp;&nbsp; Unit Price</B></TD><TD
width="132"><B>Number of Units</B></TD><TD width="170"><B>&nbsp;
  Total Price</B></TD>
</TR>
<TR>
<TD width="125">$<INPUT TYPE="TEXT" NAME="txtPrice" SIZE=10 VALUE=""
OnChange=DoRequired(this.name,this.value)></TD>
<TD width="132"><INPUT NAME="txtUnits" SIZE=10 VALUE=""
OnChange=DoRequired(this.name,this.value)></TD>
<TD width="170">$<INPUT NAME="txtTotal" SIZE=15 VALUE=""
OnChange=DoRequired(this.name,this.value)></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy