PDA

View Full Version : Having troubles with Decimals



smudly
04-11-2011, 01:31 AM
First problem:
I have a script that allows ONLY numbers to be typed into a text field. I have tried to modify this script to allow Decimals (periods) to be typed as well, but haven't succeeded. The code in Red are the changes I added to this script, but it isn't working out.


function numbersOnly(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 110 || charCode > 110))
return false;

return true;
}

Second Problem:
I need to set a Minimum number users may type in the text field. Normally this would be a simple task, but the minimum number they may type is a decimal (0.10). The default value in the field is .50. I tried replacing the .50 with .09, and I can't get the field to run the script and set its new value to .10 (<- this is the minimum). Here is the code I am working with.


<?php

$memberType = "free";
$creditsBalance = .5;
$bannerBalance = 1000;
$textBalance = 1000;

?>
<html>
<head>
<title>Convert Credits</title>
<style type="text/css">
#convert{
height: 10px;
}
</style>
<script type="text/javascript" language="javascript">
function numbersOnly(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 110 || charCode > 110))
return false;

return true;
}

function convert(){

creditsBanner = Number(document.convertform.creditsbanner.value);
creditsText = Number(document.convertform.creditstext.value);
bannerText = Number(document.convertform.bannertext.value);
textBanner = Number(document.convertform.textbanner.value);

memberType = "<?php echo $memberType;?>";
creditsBalance = "<?php echo $creditsBalance;?>";
bannerBalance = "<?php echo $bannerBalance;?>";
textBalance = "<?php echo $textBalance;?>";

if(memberType=="free"){
// If Free

// check credit balance
if(creditsBanner>creditsBalance){
document.convertform.creditsbanner.value = creditsBalance;
creditsBanner = creditsBalance;
if(creditsBanner<.1){
document.convertform.creditsbanner.value = 0.1;
creditsBanner = 0.1;
}
}
if(creditsText>creditsBalance){
document.convertform.creditstext.value = creditsBalance;
creditsText = creditsBalance;
if(creditsText<.05){
document.convertform.creditstext.value = 0.05;
creditsText = 0.05;
}
}
// check banner balance
if(bannerText>bannerBalance){
document.convertform.bannertext.value = bannerBalance;
bannerText = bannerBalance;
if(bannerText<.5){
document.convertform.bannertext.value = .5;
bannerText = .5;
}
}
// check text balance
if(textBanner>textBalance){
document.convertform.textbanner.value = textBalance;
textBanner = textBalance;
if(textBanner<2){
document.convertform.textbanner.value = 2;
textBanner = 2;
}
}

crBanner = Number(creditsBanner * 10);
crText = Number(creditsText * 20);
bnText = Number(bannerText * 2);
txBanner = Number(textBanner / 2);

document.getElementById("creditstobanner").innerHTML="<b>"+crBanner+"</b>";
document.getElementById("creditstotext").innerHTML="<b>"+crText+"</b>";
document.getElementById("bannertotext").innerHTML="<b>"+bnText+"</b>";
document.getElementById("texttobanner").innerHTML="<b>"+txBanner+"</b>";

}
else{
// If Upgraded



}



}



// function updateValues(val) {
// document.getElementById('bannerimp').value = val * 10;
// document.getElementById('field2').value = val * 20;
// document.getElementById('field3').value = val / 2;
// document.getElementById('field4').value = val * 2;
// }

//document.getElementById('creditstobanner').innerHTML

</script>
</head>
<body>
<h1>Convert Credits</h1><br />
Credits: <?php echo $creditsBalance; ?><br />
Banner Impressions: <?php echo $bannerBalance; ?><br />
Text Ad Impressions: <?php echo $textBalance; ?><br />

<form action="convert.php" method="POST" name="convertform">
<h2>Convert Your Credits to Banner or Text Ad Impressions</h2>

<div id="convert">Convert <input type="text" name="creditsbanner" size="5" onKeyUp="convert();" onKeyPress="return numbersOnly(event);"> Credits to <span id="creditstobanner"><b>0</b></span> Banner Impressions!</div> <br />

<div id="convert">Convert <input type="text" name="creditstext" size="5" onKeyUp="convert();" onKeyPress="return numbersOnly(event);"> Credits to <span id="creditstotext"><b>0</b></span> Text Ad Impressions!</div><br />

<h2>Convert Your Impressions</h2>

<div id="convert">Convert <input type="text" name="bannertext" size="5" onKeyUp="convert();" onKeyPress="return numbersOnly(event);"> Banner Impressions to <span id="bannertotext"><b>0</b></span> Text Ad Impressions!</div><br />

<div id="convert">Convert <input type="text" name="textbanner" size="5" onKeyUp="convert();" onKeyPress="return numbersOnly(event);"> Text Ad Impressions to <span id="texttobanner"><b>0</b></span> Banner Impressions!</div><br />

</form>

</body>
</html>

coothead
04-11-2011, 07:49 AM
Hi there smudly,

you may be able to adapt this example to your requirements...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title>numbers greater than or equal to 0.1</title>

<style type="text/css">

</style>

<script type="text/javascript">

function init(){

var el=document.forms[0][0];

el.onblur=function(){
if(isNaN(el.value)){
alert('numbers only');
el.value='';
el.focus();
}
if(el.value<0.1){
alert('this value must be equal to or greater than 0.1');
el.value='';
el.focus();
}
}
}

window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);

</script>

</head>
<body>

<form action="#">
<div>
<input type="text">
</div>
</form>

</body>
</html>

...or not. ;)

coothead