/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] 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&#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>
[/code]

The parseint functions seems to delete the part after the . but I don’t know how to fix the problem

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameAug 16.2011 — Try it like this:
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 = "&lt;strong&gt;Taksit / Ay :&lt;/strong&gt;" + ayliktaksit1;
document.getElementById('geriodeme').innerHTML = "&lt;strong&gt;Toplam Geri Ödeme :&lt;/strong&gt;" + geriodeme1;
}
Copy linkTweet thisAlerts:
@telmessosauthorAug 16.2011 — Thanks astupidname,

In other words, "Te&#351;ekkürler". It works like charm.

Regards

telmessos
×

Success!

Help @telmessos spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...