Urgent: HttpRequest
View Answers
July 27, 2009 at 5:28 PM
Hi Friend,
Try the following code:
1)numbers.jsp
<html>
<script language="javascript">
function isNumber(str){
if(/^-?\d+(\.\d+)?$/.test(form.num1.value)){
return true;
}
if(/^-?\d+(\.\d+)?$/.test(form.num2.value)){
return true;
}
return false;
}
function validate(){
var number1=document.form.num1.value;
var number2=document.form.num2.value;
if (isNumber(number1)==false){
form.num1.focus();
alert("Please Enter only numbers");
return false;
}
if (isNumber(number2)==false){
form.num2.focus();
alert("Please Enter only numbers");
return false;
}
}
</script>
<form name="form" method="post" onsubmit="return validate();" action="add.jsp" >
<table>
<tr><td>Enter First Number:</td><td><input type="text" name="num1"></td></tr>
<tr><td>Enter Second Number:</td><td><input type="text" name="num2"></td></tr>
<tr><td><input type="submit" value="Check" ></td></tr>
</form>
</html>
2) add.jsp
<%String num1=request.getParameter("num1");
String num2=request.getParameter("num2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a+b;
out.println("Addition of two numbers is: "+c);
%>
Thanks
Related Tutorials/Questions & Answers: