Javascript Numeric validation with 5 decimal and 3 fraction digits.

Tamilmaran
Posted by Tamilmaran under C# category on | Views : 4654

<script type="text/javascript">
function pricevalidate(test1)
{
var dec="";
var fra="";
var i;
var val=test1.value;
var l = test1.value.length;
var res="";
var dl=0;
var fl=0;
var index_of_dot;
var index_of_dot=val.indexOf('.');
var check=0;
if (index_of_dot ==-1)
dl=l;
else
{
dl=index_of_dot;
fl =(l-(index_of_dot))-1;


for (i=index_of_dot+1 ;i<l;i++)
{
check++;
if (check <4)
{ var schar=val.charAt(i);
fra+=schar ;
}
else
{
alert ("Fraction point value should be upto 3 digit");
break;
}

}

}

for (i=0;i<dl;i++)
{
if (i==5)
{
alert ("Decimal Place length should be upto 5 digit");
break;
}
var schar=val.charAt(i);
dec+=schar ;

}

if (index_of_dot !=-1)
{
if (isNaN (dec)||isNaN (fra))
{
alert ("plz enter Numeric value");
}
else
test1.value=dec+"."+fra;
}
else
{
if (isNaN (dec))
{
alert ("Plz Enter Numeric value");
}else
test1.value=dec;
}
}
</script>


Aspx code



<asp:TextBox ID="price1" Text='<%# Bind("price1") %>' MaxLength ="8" Width="60" onkeyup ="pricevalidate(this);"
TabIndex="37" autocomplete="off" runat="server" />

Comments or Responses

Login to post response