Link to home
Start Free TrialLog in
Avatar of bosscat
bosscatFlag for Malaysia

asked on

Javascript : Enter Maximum value, minimum value and certain variables in textbox

hey all,

Below are the javascript to check on the textbox value. The textbox only allow user to enter 0 and the maximum value 100 but I also need to allow the user to enter '-' and 'X'. How do I modify the code to suits the criteria?

<input type="text" class="text-input small-input" maxlength="3" size="1" name="tov[]" value="0" onkeyup="this.value = minmax(this.value, 0, 100)"/> 


<script type="text/javascript"> 
function minmax(value, min, max) 
{
    if(parseInt(value) < 0 || isNaN(value)) 
        return 0; 
    else if(parseInt(value) > 100) 
        return 100; 
	  else return value;
}
</script>

Open in new window


Thanks.
Avatar of experts1
experts1

Edit function as below:
<script type="text/javascript"> 
function minmax(value, min, max) 
{
    if(value=="-") return "-";
    if(value=="x" || value=="X") return "X";

    if(parseInt(value) < 0 || isNaN(value)) 
        return 0; 
    else if(parseInt(value) > 100) 
        return 100; 
	  else return value;
}
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of experts1
experts1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bosscat

ASKER

thanks!! it works perfectly!