SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    Code:
    <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

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by telmessos View Post
    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.
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks,

    The working code is below:


    Code:
     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;
        }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •