Ghugu Ghugu - 5 months ago 26
HTML Question

how to make input to except only from 0.0 to 5.0

I am new in javascript and don't know what to in this case please provide me solution

<input type="text" name="technical" maxlength="3" id="numa" onKeyUp="this.value = minmax(this.value, 0, 5)" class="input-text persent managementskill" value="<?=set_value('technical',$this->input->post('technical'))?>" />

<script type="text/javascript">
function minmax(value, min, max)
{

if(parseInt(value) < min || isNaN(value))
return 0;
else if(parseInt(value) > max)
return 5;
else return value;

}
</script>


This is my code is working fine for the first entry of input it will not except after 5 but I want if we enter 5.1 then it automatically converts that num to 5.0 want this to except only 0.0 to 5.0
what to do in my function please help

Answer

Use parseFloat() instead. parseInt() will convert 5.1 to 5.

Also isNaN() should be checked before parsing the value.

So your function code would look something like this,

function minmax(value, min, max) 
{

    if(isNaN(value) || parseFloat(value) < min) 
        return 0; 
    else if(parseFloat(value) > max) 
        return max; 
    else return value;

}

http://www.w3schools.com/jsref/jsref_parsefloat.asp