2016-04-08 10 views
0

Ich möchte mit Javascript eine Benutzereingabe in HTML für Float mit 2 Dezimalstellen überprüfen. Nur 0-9 und 1 x ',' sollten erlaubt sein. Wenn es kein ',' gibt, möchte ich ', 00' einfügen.Validate Eingabeformular für float mit 2 Dezimal

Zum Beispiel:

1234 -> 1234,00

123f -> 123,00

1234,12 -> 1234,12

123a4,15 -> 1234, 15

1234,1525,23 -> 12341525,23

Groß wäre zusätzlich eine separat oder viertausend, aber das ist schön

12341525,23 zu haben -> 12.341.525,23

Dieser Grund, kann ich nicht verwenden:

<input type="number" name="abc" step="0.01"> 
+0

Versuchen Sie, Eingaben zu verifizieren oder Eingaben zu transformieren? –

+0

Großartig. Danke Louys Patrice Bessette und Joels Elf. Es klappt! – t18935

Antwort

1

Dies wird überprüfen, dass es nicht mehr als ein Komma. Wenn nach dem Komma kein Komma oder nicht genügend Ziffern vorhanden sind, werden Komma und Nullen hinzugefügt. Es überprüft auch, dass die Bruch- und Integralteile Zahlen sind.

x = document.getElementById("abc").value; 
if(x.indexOf(",") != x.lastIndexOf(",")) { 
    //Invalid, there is more than one comma 
} else { 
    var comma = x.indexOf(","); 
    if(comma == -1) { 
    x = x.concat(",00"); // No comma 
    } else if(comma == x.length - 1) { 
    x = x.concat("00"); // comma, no digits after it though 
    } else if(comma == x.length - 2) { 
    x = x.concat("0"); // comma with 1 digit after it 
    } 
    if(comma <= x.length - 3) { 
    var parts = x.split(","); 
    if(isNaN(parts[0]) || isNaN(parts[1])) { 
     //Invalid, either the integral part or the fractional part is not a number 
    } 
    } else { 
    // Invalid, the comma comes before more than two digits 
    } 
} 
1

Ich denke, dass dieser Code ist, was Sie wollen. Es setzt auch ein Tausendertrennzeichen.

Beachten Sie, dass das Dezimaltrennzeichen in der Regel ein Punkt und das Tausendertrennzeichen in der Regel ein Komma ist. ;)

Vielleicht gibt es eine kürzere Möglichkeit, es nur mit regulären Ausdrücken zu tun ... Aber der Code unten funktioniert.

<input id="foo" type="text" onchange="formatNumber();" onkeyup="onlyNumbers();"> 
<script> 
function formatNumber(){ 
    // define separators 
    thousandSep="."; 
    decimalSep=","; 

    // get the input 
    n=document.getElementById("foo").value; 

    // Format decimals 
    n=parseFloat(n).toFixed(2); 
    decimals=decimalSep+n.substr(n.length-2); 

    // Temporarly remove the decimals and count how many thousand groups there is 
    n=parseInt(n).toString(); 
    thousandGroups=parseInt(Math.ceil(n.length/3)); 

    //Place the thousand separator between each group of 3 digits 
    for(i=1;i<thousandGroups;i++){ 
     j=i-1; 
     n=n.substr(0,n.length-(3*i+j))+thousandSep+n.substr(n.length-(3*i+j)); 
    } 

    // return the formated number 
    document.getElementById("foo").value=n+decimals; 
} 

function onlyNumbers(){ 
    n=document.getElementById("foo").value; 

    // Check if last keyboard input is a digit... Accepts dots 
    if(n.substr(n.length-1).search(/^[\d\.]/i)!=-1){ 
     document.getElementById("foo").value=n; 
    }else{ 
     document.getElementById("foo").value=n.substr(0,n.length-1) 
    } 
} 
</script>