How to check if variables are integer

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&#305;" onfocus = "if (this.value == 'Kredi Miktar&#305;') this.value = '';"  onblur = "if (this.value == '') this.value = 'Kredi Miktar&#305;';" />
                           
	<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&#305;" onfocus = "if (this.value == 'Faiz Oran&#305;') this.value = '';"  onblur = "if (this.value == '') this.value = 'Faiz Oran&#305;';" />
                            
<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

Those can be done in reverse order. You can replace any commas with full stops, with string.replace(‘,’, ‘.’)

Then, you can use parseFloat(string) to retrieve a numeric version of the string, and use isNaN to check if it’s a valid number or not.

Many thanks,

The working code is below:


 function Hesapla() {
        //use var statement to localize variables
        var yuzdefaiz, ayliktaksiti, geriodeme1,
            //since I assume you want to allow floating point numbers (1.5 style) or integers
            //use the Number constructor function to try and convert numeric string to Number
            //oh yeah, plus a replace of ',' with '.'
            kredimiktari = Number(document.hesapla.miktar.value.replace(/,/, '.')),
            kredivadesi = Number(document.hesapla.vade.value.replace(/,/, '.')),
            faiz = Number(document.hesapla.faiz.value.replace(/,/, '.'));
        //check for valid numbers
        if (isNaN(kredimiktari) || isNaN(kredivadesi) || isNaN(faiz)) {
            alert('Giris, Geçersiz!');
        }
        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;
    }