hi,
use the following code. This might solve your problem:
- <input type="text" onFocus="this.focus();" onBlur="checkDecimal(this,this.value)" value="" />
the javascript function checkDecimal is below:
- function checkDecimal(obj, objStr){
-
var objNumber;
-
if(isNaN(objStr) && objStr!=''){
-
alert('Value entered is not numeric');
-
objNumber = '0.00';
-
}
-
else if(objStr==''){
-
objNumber = '0.00';
-
}
-
else if(objStr.indexOf('.')!=-1){
-
if(((objStr.length) - (objStr.indexOf('.')))>3){
-
objStr = objStr.substr(0,((objStr.indexOf('.'))+3));
-
}
-
if(objStr.indexOf('.')==0){
-
objStr = '0' + objStr;
-
}
-
var sLen = objStr.length;
-
var TChar = objStr.substr(sLen-3,3);
-
if(TChar.indexOf('.')==0){
-
objNumber = objStr;
-
}
-
else if(TChar.indexOf('.')==1){
-
objNumber = objStr + '0';
-
}
-
else if(TChar.indexOf('.')==2){
-
objNumber = objStr + '00';
-
}
-
}
-
else{
-
objNumber = objStr + '.00';
-
}
-
obj.value = objNumber;
-
}
This function will check, if the number entered is numeric, if the decimal numbers take more than two digits, if the first character is a dot, if it has just a dot at the end, or just one decimal number or both the decimal numbers or no decimal number at all.
i hope there are no errors in it.. please check the opening and closing braces, as well as the variable names.