2016-04-08 33 views
0

Javascript ile bir kullanıcı girişini 2 ondalık float için html ile kontrol etmek istiyorum. Sadece 0-9 ve 1 x ',' izin verilmelidir. Eğer ',' yoksa '00' eklemek istiyorum. Örneğin2 ondalık basamaklı float için giriş formunu doğrulayın

:

1234 -> 1234,00

123f -> 123,00

1234,12 -> 1234,12

123a4,15 -> 1234, 15

1234,1525,23 -> 12341525,23

Büyük bir separat ek olurdu veya dört bin, ama bu

12341525,23 olması güzel - ı kullanamaz, Bu nedenle> 12.341.525,23

:

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

Girişi doğrulamaya veya girişi dönüştürmeye mi çalışıyorsunuz? –

+0

Harika. Teşekkür ederim Louys Patrice Bessette ve Joels Elf. İşe yarıyor! – t18935

cevap

1

Bu olmadığını kontrol edecektir Birden fazla virgül yok. Virgül sonra virgül veya yeterince basamak yoksa, virgül ve sıfırlar ekler. Ayrıca, kesirli ve ayrılmaz parçaların sayı olduğunu da kontrol eder.

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

Bence bu kod istediğiniz gibi. Ayrıca bin ayırıcıyı ayarlar.

Ondalık ayırıcı genellikle bir nokta ve bin ayırıcı genellikle bir virgül olduğunu unutmayın. ;)

Belki de yalnızca normal ifadelerle daha kısa bir yol var ... Ancak aşağıdaki kod çalışır.

<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> 
İlgili konular