Wednesday, August 26, 2009

Allow Number With Decimal and Check Decimal

function NumberWithDecimal(object,e)//onkeyup

{

var Val=object.value;
var ValLen=object.value.length;
var allowChar=Numbers+".";
/*var LastVal=Val.substring(ValLen-1);
if(allowChar.indexOf(LastVal) > -1)

{

if(LastVal=='.')

{

if(ValLen==1)

{

object.value= "0.";

}

}

}

else

{

object.value=Val.substring(0,Val.indexOf(LastVal));

}*/

for (i=0; i<ValLen; i++)

{

if(allowChar.indexOf(Val.charAt(i)) > -1)

{

if(Val.charAt(i)=='.')

{

if(i==0)

{

object.value= "0.";

break;

}

else
if
((Val.indexOf("."))!=(Val.lastIndexOf(".")))

{

object.value=Val.substring(0,Val.indexOf(".",Val.indexOf(".")+1));

break;

}

}

}

else

{

object.value=Val.substring(0,i);

break;

}

}

}

function CheckDecimal(object,e,precision)//onblur

{

var valueLen = object.value.length;

if(valueLen!=0)

{

var Val = new Number(ReplaceChar(object.value,Numbers+'.',''));

object.value=Val.toFixed(precision);

}

}

<input name="txtDecima1" type="text" id="txtDecimal1"
onkeyup="javascript:NumberWithDecimal(this,event);"
onblur="javascript:CheckDecimal(this,event,3);"
/>

No comments:

Post a Comment