Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

numbers only question

Status
Not open for further replies.

ruler1

MIS
Feb 19, 2007
89
hope i am posting in the right place. i have a byte calculator which works fine appart from it eccepts any input. i was wondering if anyone knows of a way to stop it from eccepting anything other than numbers? here is the code i am using below.

<tr>
<td class="sect1" colspan="2"> <a name="gbottom"></a><span class="hlmain">Share Calculator<br />Convert Kib/Mib.. to Bytes</span></td>
<td class="sect1"><span class="postbody">
<input type="text" name="bytev" size="20" maxlength=11 value="enter value" onfocus="document.edithub.bytev.value=''">
<select size="1" name="units">
<option value="Bytes">Bytes</option>
<option value="Kib">Kib</option>
<option value="Mib">Mib</option>
<option value="Gib">Gib</option>
<option value="Tib">Tib</option>
<option value="Pib">Pib</option>
</select>
<input type="button" value="Calculate" name="B1" onClick="calculate()"><br />
<input type="text" name="output" size="25" maxlength="25" value="results here"> <--- this is the number you need
<script>
var bytevalue=0
function calculate(){
var invalue=document.edithub.bytev.value
var selectunit = document.edithub.units.options[document.edithub.units.selectedIndex].value
if (selectunit=="Bytes")
bytevalue=invalue
else if (selectunit=="Kib")
bytevalue=invalue*1024
else if (selectunit=="Mib")
bytevalue=invalue*1024*1024
else if (selectunit=="Gib")
bytevalue=invalue*1024*1024*1024
else if (selectunit=="Tib")
bytevalue=invalue*1024*1024*1024*1024
else if (selectunit=="Pib")
bytevalue=invalue*1024*1024*1024*1024*1024
document.edithub.output.value = bytevalue
}
</script></span></td>
</tr>

i'd be greatful for any help with this. thanks
 
There are many ways to do this, but I'd say go with something simple, such as alerting the user if their input contains anything that cannot be parsed as a number.

You can do this with the "isNaN" function, which returns true if the value passed to it is non-numerical.

You might also want to employ "parseInt" or "parseFloat" depending on your requirements, but remember if you use the former, don't forget to add the extra base 10 parameter to avoid unexpected results.

P.S. Why don't you use proper abbreviations for your units? For example, "Kib" and "Mib" should be "kB" and "MB" (note: not "Mb").

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
i know really next to nothing about javascript so im not too sure what extra code i need or how to write it :(
i found the code somewhere on google and implimented it into the page i am using hence the reason for MiB and KiB etc.. i thought kB,MB... was changed recently but maybe i am wrong? thanks
 
no need to answer this question anymore, i have now solved this issue.
thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top