Hi,
I have the following Javascript code to make a simple calculation. It works fine the numbers entered are integer. But if the numbers are 1.5 style I get stupid results or NAN error.
I would like to add a check to the script which checks:
1- If the value entered is integer or 1.5 style for all fields. If it is not gives an alert message and stop calculating.
2- If the visitor enters 1,5 instead of 1.5 (In Turkey it is a common usage.) I need script replace the 1,5 with 1.5 and then make the calculation.
Thank you in advance
telmessos
<html>
<head>
<script type="text/javascript">
function Hesapla() {
kredimiktari = parseInt(document.hesapla.miktar.value);
kredivadesi = parseInt(document.hesapla.vade.value);
faiz = parseInt(document.hesapla.faiz.value);
yuzdefaiz = faiz / 100;
ayliktaksit1 = ((kredimiktari * yuzdefaiz) + kredimiktari) / kredivadesi;
geriodeme1 = ((kredimiktari * yuzdefaiz) + kredimiktari);
document.getElementById('ayliktaksit').innerHTML = "<strong>Taksit / Ay :</strong>" + ayliktaksit1;
document.getElementById('geriodeme').innerHTML = "<strong>Toplam Geri Ödeme :</strong>" + geriodeme1;
}
</script>
</head>
<body>
<form name="hesapla">
<input type="text" name="miktar" value="Kredi Miktarı" onfocus = "if (this.value == 'Kredi Miktarı') this.value = '';" onblur = "if (this.value == '') this.value = 'Kredi Miktarı';" />
<input type="text" name="vade" value="Kredi Vadesi" onfocus = "if (this.value == 'Kredi Vadesi') this.value = '';" onblur = "if (this.value == '') this.value = 'Kredi Vadesi';" />
<input type="text" name="faiz" value="Faiz Oranı" onfocus = "if (this.value == 'Faiz Oranı') this.value = '';" onblur = "if (this.value == '') this.value = 'Faiz Oranı';" />
<div id="gosterim"><span id="ayliktaksit"></span><span id="geriodeme"></span></div>
<input type='button' value='Hesapla' onClick='Hesapla()'><br><br>
<img src="http://www.sitepoint.com/forums/images/dot2.png" width="180" height="2" alt="dot"/>
</form>
</body>
</html>
The parseint functions seems to delete the part after the . but I don’t know how to fix the problem