Pagina 2 di 2 primaprima 1 2
Visualizzazione dei risultati da 11 a 18 su 18
  1. #11
    Utente di HTML.it
    Registrato dal
    Sep 2007
    Messaggi
    1,171
    ma se sono di meno di 7 non funziona???

  2. #12
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Devi modificarlo in base a quanti realmente sono.

  3. #13
    Utente di HTML.it
    Registrato dal
    Sep 2007
    Messaggi
    1,171
    li ho modificati in base ai nomi dei campi ma nn funzionano.

  4. #14
    Utente di HTML.it
    Registrato dal
    Apr 2000
    Messaggi
    938
    Ciao,

    riprendo http://forum.html.it/forum/showthrea...readid=1465037

    dove la tua richiesta era il perchè 12.10 - 10.00 ti riportava 2.10000000 e non 2.10

    e risposta hai avuto.

    <->

    numeri Decimali:

    numeri compresi da 0 e 9 (set , (0-9)), es: 0, 1, 125, 2500 ecc..

    numeri EsaDecimali:

    numeri compresi tra 0 e 9 (set), lettere A B C D E F (A=10, B=11, C=12 D=13, E=14, F=15)

    numeri Binari:

    numeri compresi tra 0 e 1 es: 1111101

    numeri Ottali:

    numeri compresi tra 0 e 7



    ES: Decimale 125:

    DECIMALE 125
    ESADECIMALE = 7D
    BINARIO = 1111101
    OTTALE = 175

    <->


    Il metodo toFixed() restituisce una stringa (da un oggetto Number()) formattato al numero di cifre decimali fornite come parametro.

    Il valore di ritorno di toFixed è stringa.

    Se fai una operazione 1000000.20 - 1000000.10 il risultato è 0.10, ma non puoi scriverla cosi 1.000.000.20 - 1.000.000.10

    risultato NAN, Not a Number, richiesta su un'operazione che non è un numero.


    Se nell'operazione + - / * che sia metti il . (che per le operazioni matematiche in javascript è il solo accettato),
    o fai in modo che nel risultato il . non venga considerato o quando scrivono nel campo lo cancelli.


    Nell'esempio postato (che funziona correttamente) se scrivi 1.000.00 onblur diventa 1.00 ecc...

    Numeri eccedenti es: 20 1 (uno) 11111111111111111111 diventa 11111111111111110656.00

    Quindi se il tuo scopo è il calcolo di cifre in denaro ES: EURO dove per i decimali si usa la , e non il .

    1.000,70 € devi fare modifiche.


    ES: se vuoi fare questa operazione 5+7/2 5+7 = 12 / 2 = 6 ERRATO fa 8.5


    M.

  5. #15
    Ti merita generalizzare il più possibile in maniera da non dover nemmeno stare a scrivere il gestore di evento dentro ogni tag.

    Per cui premesso che i campi sono di testo, modificagli i nomi: nello script che ti metto qui i nomi sono del tipo:
    importo2, importo8 (boh m'è parso di capire il tuo html era più o meno così, ma non ha importanza: se non lo è, riadattalo, è roba da poco specie se i campi sono una manciata)

    cioè il name è la stringa 'importo' più un numero. Qualunque altro campo di testo nelle form che non sia di type text e non abbia un name settato ad importoNUMERO non verrà influenzato dallo script.
    A questo punto non ha importanza quanti campi sono, se 3, zero 7, 50 o forse sì forse no - per questo dicevo ti merita generalizzare.

    Tieni presente che siccome sei il programmatore di questa pagine, hai pieno controllo sull' ambiente di sviluppo e sapere che la tara=importo3 e che so lordo=importo4 non dovrebbe costituire un problema per te. Ovviamente si potrebbe riadattare tutto per uno scenario diverso, ma io prefersco una soluzione più semplice.

    codice:
    <form><div>
    <input type="text" name="importo1" />
    
    <input type="text" name="importo2" />
    
    <input type="text" name="importo3" />
    
    <input type="text" name="campoConNameNonTipo_importoNUM" />
    
    <input type="text" name="importo4" />
    
    <input type="text" name="importo5" />
    
    <input type="text" name="importo6" />
    
    <input  name="importo7" />
    </div></form>
    
    
    
    <script>
    function assegnaFunzione(){
    var els=document.getElementsByTagName('input');
    if(els){
    	for(var i=0; i<els.length; i++){
    	if(/^importo[\d]+$/.test(els[i].name) && els[i].type=='text'){
    	els[i].addEventListener('blur', function(){if(this.value && !/^[\d.,]+$/.test(this.value)){this.value='';}; this.value=(this.value)?parseFloat(this.value.replace(',', '.')).toFixed(2):'';}, true);
    	};
    	}
    };
    }
    
    assegnaFunzione();
    </script>
    L'html della form è esemplificativo.
    Questo ti funziona su tutti i browser, ti corregge eventuali virgole messe al posto del punto (js usa il punto per i decimali, non la virgola), evita la visualizzazione del value in caso di NaN, e ti resetta a un campo vuoto i campi non numerici. Opera onblur.
    vedi te se fa al caso tuo, non tutti i passaggi delle tue esigenze sono chiarissimi.

    ps se ti serve resettare tutti i valori non numerici a 0.00 o che so io, si ottiene facilmente con una minima modifica del codice

    if(!this.value || !/^[\d.,]+$/.test(this.value)){this.value='0.00';}

  6. #16
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    A questo punto io farei così, in modo tale che il nome del campo non conti...
    codice:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="it" />
    <title>Esempio per il Forum di HTML.it</title>
    <script type="text/javascript">
    function checkInput(oMyField, oKeyEvent) {
    	var nChar;
    	if (window.event) { nChar = window.event.charCode; }
    	else if (oKeyEvent) { nChar = oKeyEvent.charCode; }
    	else { return true; }
    	var sChar = String.fromCharCode(nChar), rComma = /(\.|,)/;
    	if (nChar === null || nChar === 0 || nChar === 8 || nChar === 9 || nChar === 13 || nChar === 27) { return true; }
    	if (/\d+/.test(sChar) || (rComma.test(sChar) && !rComma.test(oMyField.value))) { return true; }
    	return false;
    }
    
    function checkValue(oMyField) {
    	var nParsedValue = parseFloat(oMyField.value.replace(/,/g, ".").replace(/(\.)+$/, ""));
    	if (isFinite(nParsedValue)) { oMyField.value = nParsedValue.toFixed(2); }
    }
    </script>
    </head>
    
    <body>
    
    <form name="form1">
    
    
    <input type="text" name="importo1" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" />
    
    <input type="text" name="importo2" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" />
    
    <input type="text" name="importo3" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" />
    
    <input type="text" name="importo4" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" />
    
    <input type="text" name="importo5" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" />
    
    <input type="text" name="importo6" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" />
    
    <input type="text" name="importo7" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" /></p>
    </form>
    
    </body>
    </html>
    @TrueLies
    Se vuoi possiamo continuare la nostra discussione teorica su come gestire al meglio la memoria (mi riferisco del tuo esempio)...
    https://developer.mozilla.org/en/Jav...considerations

  7. #17
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Mmmmmh... meglio così:
    codice:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="it" />
    <title>Esempio per il Forum di HTML.it</title>
    <script type="text/javascript">
    function checkInput(oMyField, oKeyEvent) {
    	var nChar;
    	if (window.event) { nChar = window.event.charCode; }
    	else if (oKeyEvent) { nChar = oKeyEvent.charCode; }
    	else { return true; }
    	var sChar = String.fromCharCode(nChar), rComma = /(\.|,)/;
    	if (nChar === null || nChar === 0 || nChar === 8 || nChar === 9 || nChar === 13 || nChar === 27) { return true; }
    	if (/\d/.test(sChar) || (rComma.test(sChar) && !rComma.test(oMyField.value))) { return true; }
    	return false;
    }
    
    function checkValue(oMyField) {
    	var nParsedValue = parseFloat(oMyField.value.replace(/,/g, ".").replace(/(\.)+$/, ""));
    	if (isFinite(nParsedValue)) { oMyField.value = nParsedValue.toFixed(2); }
    }
    </script>
    </head>
    
    <body>
    
    <form name="form1">
    
    
    <input type="text" name="importo1" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="importo2" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="importo3" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="importo4" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="importo5" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="importo6" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="nomeACaso" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" /></p>
    </form>
    
    </body>
    </html>

  8. #18
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Maledetta fretta... c'era del codice di troppo nell'esempio precedente...
    codice:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="it" />
    <title>Esempio per il Forum di HTML.it</title>
    <script type="text/javascript">
    function checkInput(oMyField, oKeyEvent) {
    	if (!oKeyEvent) { oKeyEvent = window.event; }
    	var nChar = oKeyEvent.charCode, sChar = String.fromCharCode(nChar), rSeparator = /(\.|,)/;
    	if (nChar === 0 || /\d/.test(sChar) || (rSeparator.test(sChar) && !rSeparator.test(oMyField.value))) { return true; }
    	return false;
    }
    
    function checkValue(oMyField) {
    	var nParsedValue = parseFloat(oMyField.value.replace(",", "."));
    	if (isFinite(nParsedValue)) { oMyField.value = nParsedValue.toFixed(2); }
    }
    </script>
    </head>
    
    <body>
    
    <form name="form1">
    
    
    <input type="text" name="importo1" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="importo2" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="importo3" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="importo4" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="importo5" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="importo6" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" />
    
    <input type="text" name="nomeACaso" onkeypress="return(checkInput(this, event));" onblur="checkValue(this);" onpaste="return(false);" /></p>
    </form>
    
    </body>
    </html>

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.