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!

Form problem - could do with some pointers

Status
Not open for further replies.

rosie3705

Technical User
Jul 28, 2007
6
GB
Im new to JS and I need help with 3 textbox's.
count the characters in textbox1
then multiply by value of textbox2
then display result in textbox3??

(textbox1 is variable in length)
cheers

 
Try this:
Code:
<script language="JavaScript" type="text/JavaScript">
function calculate(){
	var text1 = document.forms['myForm'].elements['text1'].value;
	var text2 = document.forms['myForm'].elements['text2'].value;
	
	text1 = text1.length;
	var numExp = /^[-]?\d+(\.\d+)?$/;
	if(numExp.test(text2)){
		var result = text1 * text2;
		document.forms['myForm'].elements['text3'].value = result;
	} else {
		alert('text 2 is not a number');
	}
	
	return false;
}
</script>
Code:
<form name="myForm" action="calculator.html" method="get">
	Text 1: <input type="text" name="text1" /><br />
	Text 2: <input type="text" name="text2" /><br />
	Text 3: <input type="text" name="text3" readonly="readonly" />
	<input type="submit" value="calculate" onClick="return calculate();" />
</form>
 
It works a treat. Thanks - I owe you a pint or two
 
Hi

rosie3705, if you found solepixel's answer helpful, please reward him/her with a purple star by clicking the

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


link below his/her message, then click again to confirm on the page which will appear in the pop-up window.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top