Page 1 of 1

javascript advice on forms

Posted: Tue Sep 20, 2005 11:58 am
by kol090
Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
can you pls tell me what i have done wrong here?
i want to add input1 and input2 together and then display it in the msgbox, 
the strange thing is that it works when i want to subtract multiply and divide but not add

Code: Select all

<script type="text/javascript">
function valid(form)
{
  var input1=0;
  var input2=0;
  var tot = 0;
  input1 = document.myform.data1.value;
  input2 = document.myform.data2.value; 
  tot = input1 + input2;
  alert("Hello " + tot + "! Welcome...");
}
</SCRIPT>



Click on the button after entering your
name into the text box:<BR>

<form name="myform">
<input type="text" name="data1" value="" size=10>
<input type="text" name="data2" value="" size=10>
<INPUT TYPE="button" VALUE="Click Here" onClick="valid(this.form)">
</form>
Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Sep 20, 2005 12:18 pm
by feyd
Attributes are treated as strings.. you need to convert them to integers or floats: parseInt() or parseFloat()

Code: Select all

input1 = parseInt(document.myform.data1.value);

Posted: Tue Sep 20, 2005 1:03 pm
by Jenk
EDIT: Maybe I should read the posts properly :roll: @ me.

+ is the concatenate operator when used on strings.

Posted: Tue Sep 20, 2005 1:42 pm
by raghavan20
this might be common in many languages

Code: Select all

<script type = 'text/javascript'>
var i = 3, j = 5, k;
var l = "str";
var m = "500";

k = i + j; //returns an i nteger; output should be 8
document.write(k + "<br />");
k = 3 + l; //returns a string since one of the operands is a string; output should be 3str
document.write(k + "<br />");
k = l + m;//returns string since both operands are strings; output should be str500
document.write(k + "<br />");
k = i + m; //returns 3500
document.write(k + "<br />");
k = i + parseInt(m); //returns 503
document.write(k + "<br />");
</script>

Posted: Tue Sep 20, 2005 1:46 pm
by kol090
i tried

Code: Select all

input1 = parseInt(document.myform.data1.value);
but nothing works i can't even get one of the things in entered back forget about adding them.

Code: Select all

function sum(form) 

{
    var input1 = parseInt(document.myform.data1.value);
	var input2 = parseInt(document.myform.data2.value);
    alert("Hello " + input2 + " ! Welcome...");
}


</script>
<form>
	   <input type="text" name="data1" size ="2" onBlur="checksuma(this.value)">
       <input type="text" name="data1" size ="2" onBlur="checksumb(this.value)">
	   <input type="button" value="sum" onClick="sum(this.form)">
</form>

whats the matter?

Posted: Tue Sep 20, 2005 1:53 pm
by raghavan20
see you are getting the form object as form in the function not as myform.
so change myform in your assignment statements to form.

Posted: Tue Sep 20, 2005 2:23 pm
by John Cartwright
Moved to client-side.

Posted: Tue Sep 20, 2005 2:26 pm
by kol090
alright thanks it works now