codice:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" lang="it">
<head>
<title>Esempio di Codice</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="it" />
<script type="text/javascript" language="JavaScript">
<!--
/*
checkFloat( <riferimento all'oggetto> [, <numero di cifre decimali> ] );
*/
function checkFloat( __hItem, __decimalPos )
{
if ( __hItem == null )
return false;
if ( __decimalPos == null || __decimalPos < 0 )
__decimalPos = 0;
var decDigit = Math.pow(10, __decimalPos);
var strValue = Math.round(parseFloat(__hItem.value.replace(/,/, '.')) * decDigit ) / decDigit;
if ( isNaN(strValue) )
strValue = 0;
strValue = strValue.toString();
var decPos = strValue.indexOf('.');
if ( decPos == -1 ) {
decPos = 0;
if ( __decimalPos > 0 )
strValue += '.';
}
else
decPos = strValue.length - decPos - 1;
for (var i=0; i<__decimalPos-decPos; i++)
strValue += '0';
__hItem.value = strValue;
return true;
}
//-->
</script>
</head>
<body>
<form action="//" onsubmit="return false;">
Prezzo #1: <input type="text" onblur=" checkFloat(this, 0); " /> (senza decimali)
Prezzo #2: <input type="text" onblur=" checkFloat(this, 1); " /> (1 cifra decimale)
Prezzo #3: <input type="text" onblur=" checkFloat(this, 2); " /> (2 cifre decimali)
Prezzo #4: <input type="text" onblur=" checkFloat(this, 3); " /> (3 cifre decimali)
Prezzo #5: <input type="text" onblur=" checkFloat(this, 10); " /> (10 cifre decimali)
</form>
</body>
</html>