Home  »  Articles  »  Java Script  »

How to Validate Decimal and Currency in ASP.Net Control using Javascript

 Posted By : Manoranjan SahooPosted Date : 26/10/2010 11:22:43 AM Category : Java Script Points : 4 (Rs 1.00)

Try this code :
place this in head tag of .aspx file

CODE

function validateNumber(txtbox1)
{
     var str=txtbox1.value;
     var str1="0123456789.";
     var i=0;
     for(i=0;i<str.length;i++)
     {
          if(str1.indexOf(str.substring(i,i+1))<0 || str1.indexOf(str.substring(i,i+1))>str1.length || str.indexOf(".")!=str.lastIndexOf("."))
          {
              txtbox1.value=str.substring(0,str.length-1);
              alert("Enter Only Decimal Number");
              txtbox1.focus();
              break;
          }
      }
}
function checkDecimal(txtBox1)
{
    var str2=txtbox1.value;
    if(str2.indexOf('.')<0)
    {
       txtbox1.value=str2+".00";
    }
}

Then Place this into page_load event of that page

CODE

Box1.Attributes.Add("onKeyup","javascript:validateNumber(TextBox1)")
TextBox1.Attributes.Add("onBlur","javascript:checkDecimal(TextBox1)")

Thank You

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.