Solved

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

Posted on 2012-03-31
3
611 Views
Last Modified: 2012-08-13
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
Comment
Question by:bosscat
  • 2
3 Comments
 
LVL 9

Expert Comment

by:experts1
Comment Utility
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
 
LVL 9

Accepted Solution

by:
experts1 earned 500 total points
Comment Utility
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
 
LVL 1

Author Comment

by:bosscat
Comment Utility
thanks!! it works perfectly!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now