Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 633
  • Last Modified:

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.
0
bosscat
Asked:
bosscat
  • 2
1 Solution
 
experts1Commented:
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

0
 
experts1Commented:
To clear on backspace mod as follows:
<script type="text/javascript"> 
function minmax(e,value, min, max) 
{
    // Clear on backspace FF etc..
    if (e.which) {
    var chk = e.which;
    if (chk == 08) return "";
       }

    // Clear on backspace IE etc..
    if (e.keyCode) {
    var chk = e.keyCode;
    if (chk == 08) return ""; 
       }
       
    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

0
 
bosscatAuthor Commented:
thanks!! it works perfectly!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now