Results 1 to 2 of 2

Thread: Having troubles with Decimals

  1. #1
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having troubles with Decimals

    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.

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

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

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there smudly,

    you may be able to adapt this example to your requirements...
    Code:
    
    <!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

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
  •