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!

checking for a numeric range

Status
Not open for further replies.

JohannIcon

Programmer
Sep 3, 2002
440
MT
Hi All,

I am trying to check wether the user inputs a correct decimal number. The number must be smaller than 1 but bigger than 0. In other words, the user must input numbers like 0.01 or 0.712 for the number field to be correct. I am doing the following validation, however it is not working:-

function isNumber(inputVal)
{
inputStr = inputVal.toString()
for (var i = 0; i <inputStr.length; i++)
{
var oneChar = inputStr.charAt(i)
if (oneChar < &quot;0&quot; || oneChar > &quot;1&quot;)
{
alert(&quot;Numeric Field Here. You must input a number, like 0.712&quot;)
return false
}
}
return true
}

What am I missing?
 
Try somethiong like this

Hope it helps.

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>

<html>
<head>
<title>Number Check</title>
</head>
<script>
function checkValue(){
var e = parseFloat(document.f.t.value)

if( isNaN(e) ){
alert(&quot;Not a number&quot; );
return false;
}
else{
if( (e > 0) && ( e < 1 ) ){
alert(&quot;Digital&quot; );
return true;

}else{
alert(&quot;outside range&quot;);
return false;
}
}

}

</script>
<body>
<form action=&quot;&quot; method=&quot;post&quot; name=&quot;f&quot; id=&quot;f&quot; onsubmit=&quot;return checkValue();&quot;>
<input type=&quot;text&quot; name=&quot;t&quot;>
<input type=&quot;submit&quot; name=&quot;s&quot; value=&quot;s&quot;>
</form>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top