2012-03-19 18 views

risposta

2

Questo è probabilmente una cattiva idea ...

<!doctype html> 
<html lang="en"> 
<head> 
<meta charset= "utf-8"> 
<title>Comma Thousands Input</title> 
<style> 
label, input, button{font-size:1.25em} 
</style> 
<script> 
// insert commas as thousands separators 
function addCommas(n){ 
    var rx= /(\d+)(\d{3})/; 
    return String(n).replace(/^\d+/, function(w){ 
     while(rx.test(w)){ 
      w= w.replace(rx, '$1,$2'); 
     } 
     return w; 
    }); 
} 
// return integers and decimal numbers from input 
// optionally truncates decimals- does not 'round' input 
function validDigits(n, dec){ 
    n= n.replace(/[^\d\.]+/g, ''); 
    var ax1= n.indexOf('.'), ax2= -1; 
    if(ax1!= -1){ 
     ++ax1; 
     ax2= n.indexOf('.', ax1); 
     if(ax2> ax1) n= n.substring(0, ax2); 
     if(typeof dec=== 'number') n= n.substring(0, ax1+dec); 
    } 
    return n; 
} 
window.onload= function(){ 
    var n1= document.getElementById('number1'), 
    n2= document.getElementById('number2'); 
    n1.value=n2.value=''; 

    n1.onkeyup= n1.onchange=n2.onkeyup=n2.onchange= function(e){ 
     e=e|| window.event; 
     var who=e.target || e.srcElement,temp; 
     if(who.id==='number2') temp= validDigits(who.value,2); 
     else temp= validDigits(who.value); 
     who.value= addCommas(temp); 
    } 
    n1.onblur= n2.onblur= function(){ 
     var temp=parseFloat(validDigits(n1.value)), 
     temp2=parseFloat(validDigits(n2.value)); 
     if(temp)n1.value=addCommas(temp); 
     if(temp2)n2.value=addCommas(temp2.toFixed(2)); 
    } 
} 
</script> 
</head> 
<body> 
<h1>Input Thousands Commas</h1> 
<div> 
<p> 
<label> Any number <input id="number1" value=""></label> 
<label>2 decimal places <input id="number2" value=""></label> 
</p></div> 
</body> 
</html> 
+0

Grazie ma perché dici che probabilmente è una cattiva idea? – dames

+0

È una cattiva idea in quanto cambia il testo inviato al server. – David

2

per l'input dell'utente Raccomando di non formattare il valore per includere una virgola. Sarà molto più facile gestire un valore intero (12000) rispetto a un valore stringa (12.000) una volta inviato.

Tuttavia, se si è certi di formattare il valore, come raccomandato da @achusonline, si dovrebbe mascherare il valore. Qui è un plugin jQuery che sarebbe utile per ottenere questo risultato:

http://digitalbush.com/projects/masked-input-plugin/

+0

"molto più facile da affrontare ..." Davvero? Voglio dire, la formattazione di stripping è davvero molto semplice. Ciò che è difficile è gestire la formattazione per un pubblico multilingue. Ma, se vuoi aggiungere la formattazione nel client, eliminarla su submit è banale. –

10

provare qualcosa di simile this

function addCommas(nStr) 
{ 
    nStr += ''; 
    x = nStr.split('.'); 
    x1 = x[0]; 
    x2 = x.length > 1 ? '.' + x[1] : ''; 
    var rgx = /(\d+)(\d{3})/; 
    while (rgx.test(x1)) { 
     x1 = x1.replace(rgx, '$1' + ',' + '$2'); 
    } 
    return x1 + x2; 
} 

poi usarlo sul tuo caselle di testo in questo modo

<input type="text" id="txtBox" onchange="return addCommas(this.value)" /> 

Speranza che aiuta

+0

Grazie A Millions Guys !! thumbs up @Mark Walters – dames

+1

Per i futuri lettori, tenete a mente che questo fallisce con l'input 'type =" number "' dato che i campi del numero html5 non possono accettare una virgola –

+0

come @DarrenSweeney ha detto che 'number' non accetta la virgola. ma 'type =" tel "' farà un trucco –

6

Utilizzare il jQuery autoNumeric plugin:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <title>Input with separator</title> 
    <script type="text/javascript" src="jquery-1.11.0.js"></script> 
    <script type="text/javascript" src="autoNumeric.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     $("#myinput").autoNumeric(
     'init', {aSep: ',', mDec: '0', vMax: '99999999999999999999999999'} 
    ); 
    }); 
    </script> 
</head> 
<body> 
    <input id="myinput" name="myinput" type="text" /> 
</body> 
</html> 
Problemi correlati