calculation with validation
function Sum()
{
var txtVal1= document.getElementById('<%= txtVal1.ClientID %>').value;
var txtVal2 = document.getElementById('<%= txtVal2.ClientID %>').value;
if(txtVal1.toString().replace(/_/g,' ').trim().length==0)
{
txtVal1="0";
}
if(txtVal2.toString().replace(/_/g,' ').trim().length==0)
{
txtVal2="0";
}
var result= parseFloat(txtVal1) + parseFloat(txtVal2);
document.getElementById('<%= txtAnswer.ClientID %>').value= result ;
}
hi,,,i made one javascript as given above to calculate the value of 2 textbox and give answer in 3rd textbox ,,if i dont apply any value in a textbox it will automatcally consider value with zero..but its not working,,,give me some idea..with code,,thnx
Jitendra PatelPosted Mar 8, 2010, 4:26 AM
Use this code
function Check()
{
var txtVal1= document.getElementById('<%= TextBox1.ClientID %>').value;
var txtVal2 = document.getElementById('<%= TextBox2.ClientID %>').value;
alert(document.getElementById('<%= TextBox1.ClientID %>').value);
alert(document.getElementById('<%= TextBox2.ClientID %>').value);
if(document.getElementById('<%= TextBox1.ClientID %>').value == "")
{
document.getElementById('<%= TextBox1.ClientID %>').value="0";
txtVal1=document.getElementById('<%= TextBox1.ClientID %>').value;
alert("ji");
}
if(document.getElementById('<%= TextBox2.ClientID %>').value == "")
{
document.getElementById('<%= TextBox2.ClientID %>').value="0";
txtVal2=document.getElementById('<%= TextBox2.ClientID %>').value;
}
var result= parseFloat(txtVal1) + parseFloat(txtVal2);
alert(result);
document.getElementById('<%= TextBox3.ClientID %>').value = result;
}
If useful this code answer me.
Thanks,
Jitendra Patel