Solved

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

Posted on 2012-03-31
3
618 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
ID: 37791572
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
ID: 37792216
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
ID: 37792312
thanks!! it works perfectly!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
multiple selects 23 48
Filktering Alphabetically 8 29
Word, Excel, Access icons 4 17
How can I make the first part identical to the second ? 1 25
This article discusses four methods for overlaying images in a container on a web page
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

856 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