I am trying to create an html form for adding 2 numbers and also trying to validate using java script. what I want is instead of getting an alert msg as a result
I want the result to be displayed in the sum:text box --- not as an alert
I want to validate the input fields ,if null -- throw an alert msg
Can anyone help me withthis code plz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Addition</title>
<script type="text/javascript">
function do_addition()
{
var value1,value2,sum;
if(document.form1.first.value=="" || document.form1.first.value==null){
alert("Enter number to calculate"");
}
else if(document.form1.second.value=="" || document.form1.second.value==null){
alert("Enter number to calculate"");
}
value1 = parseInt(document.form1.first.value);
value2 = parseInt(document.form1.second.value);
sum = (value1+value2);
alert("The sum of " + value1 + " and " + value2 + " is " + sum + ".");
}
</script>
</head>
<body>
<form method="post" name="form1" onsubmit="return do_addition(this);">
Enter 1st Number:
<input type="text" name="first" size="20"/>
<br />
Enter 2nd Number:
<input type="text" name="second" size="20"/>
<br>
Sum:<input type="text" name="second" size="20"/>
<br>
<input type="submit" name="submit" value="Add" />
<br>
<br>
<input type="Reset" value="Clear">
<br>
</form>
</body>
</html>
Mod Edit: Please use code tags when posting your code. Code tags are used like so =>
Thanks,
PsychoCoder

New Topic/Question
Reply



MultiQuote






|