Regalamiunsorriso/rus/admin/_V4/_js/numberInput.js
2026-03-14 20:04:39 +01:00

178 lines
5.1 KiB
JavaScript

/*ver. 0.2
24-04-2015 gestione campo indietro
10-03-2014 checkNumber: intercettatto il punto e sostituito con virgola
24-09-2009 prima versione. tolto formatNumber e strintToNumber da function.js
*/
//////////////////////////////////////////////////////////////////////////////////7
//////////////////////////////////////////////////////////////////////////////////7
//////////////////////////////////////////////////////////////////////////////////7
function formatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
IN:
NUM - the number to format
decimalNum - the number of decimal places to format the number to
bolLeadingZero - true / false - display a leading zero for
numbers between -1 and 1
bolParens - true / false - use parenthesis around negative numbers
bolCommas - put commas as number separators.
RETVAL:
The formatted number!
**********************************************************************/
{
/*
LOCALE IT --> SEPARATORE DECIMALE , SEPARATORE MIGLIAIA .
LOCALE EN --> SEPARATORE DECIMALE . SEPARATORE MIGLIAIA ,
//da gestire
*/
var sepD=",";
var sepM=".";
if (isNaN(parseInt(num))) return "NaN";
var tmpNum = num;
var iSign = num < 0 ? -1 : 1; // Get sign of number
// Adjust number so only the specified number of numbers after
// the decimal point are shown.
tmpNum *= Math.pow(10,decimalNum);
tmpNum = Math.round(Math.abs(tmpNum))
tmpNum /= Math.pow(10,decimalNum);
tmpNum *= iSign; // Readjust for sign
// Create a string object to do our formatting on
var tmpNumStr = new String(tmpNum);
//alert(tmpNumStr);
//se locale=IT sostituisco i . con le ,
tmpNumStr=tmpNumStr.replace('.',',');
// See if we need to strip out the leading zero or not.
if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
if (num > 0)
tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
else
tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
// See if we need to put in the commas
if (bolCommas && (num >= 1000 || num <= -1000)) {
var iStart = tmpNumStr.indexOf(sepD);
if (iStart < 0)
iStart = tmpNumStr.length;
iStart -= 3;
while (iStart >= 1) {
tmpNumStr = tmpNumStr.substring(0,iStart) + sepM + tmpNumStr.substring(iStart,tmpNumStr.length)
iStart -= 3;
}
}
// See if we need to use parenthesis
if (bolParens && num < 0)
tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";
return tmpNumStr; // Return our formatted string!
}
function formatNumb(num,decimalNum)
{
// versione semplificata
return formatNumber(num,decimalNum,true,false,false);
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
function checkNumber(i, delKey,direction) {
// utilizzare sui fild numerici
//onkeydown="checkNumber(this, event.keyCode,'down')" onkeyup="checkNumber(this, event.keyCode,'up');refreshResto()"
if (i.value.length < 10) {
if (delKey!=9) { //tab
if(delKey!=8 && delKey!=46 && delKey!=16 && !(delKey>36 && delKey<41)){
//if the delete, backspace, shift, are not the keys that caused the keyup event.
if ((delKey >= 48 && delKey <= 57) || (delKey >= 96 && delKey <=105) || (delKey==188) || (delKey==112)) {
//tutto ok....
//190 -->, 110 -->.
} else if(delKey==190 || delKey==110) {
i.value = i.value.substring(0,i.value.length-1);
//alert(i.value.indexOf(","));
if (i.value.indexOf(",")==-1) {
i.value += ",";
}
} else {
if (direction == "up") {
if (i.value.length == 0) {
i.value = "";
} else {
i.value = i.value.substring(0,i.value.length-1);
}
}
}
i.focus();
}
} else {
if (direction == "down") {
return checkNumber(i)
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////7
//////////////////////////////////////////////////////////////////////////////////7
//////////////////////////////////////////////////////////////////////////////////7
function stringToNumber(val)
{
//levo ,00 eventuale
var v00=val.indexOf(',00');
if(v00!=-1)
val=val.substr(0,v00);
//levo il , finale eventuale
var virg=val.indexOf(',');
if(virg!=-1 &&virg==val.length-1 )
val=val.substr(0,virg);
//aggiustamenti punti
val=val.replace(/[.]/,'');
val=val.replace(/[.]/,'');
val=val.replace(/[.]/,'');
val=val.replace(/[.]/,'');
val=val.replace(',','.');
//tolgo gli zeri finali
var dotIdx=val.indexOf('.');
//alert("dotidx0"+dotIdx);
if(dotIdx!=-1)
{
var j=val.length-1;
/* while(val.charAt(i)!='.' && val.charAt(i)=='0' ) NON FUNZIONA.. forse perche' usavo i invece di j!!*/
while(val.charAt(j)!='.' )
{
//alert("j="+j+" val charat(j):"+val.charAt(j));
if(val.charAt(j)=='0')
{
val=val.substr(0,j);
j--;
}
else j=dotIdx;
}
if(val.charAt(val.length-1)=='.')
val=val.substr(0,val.length-1);
}
num = parseFloat(val);
return num;
}