Solved

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

Posted on 2012-03-31
3
619 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
curl parse data from site 20 36
Table style position to the left 8 24
Ajax on ASP 2 33
Hit counters in SharePoint 2013 2 21
This article discusses how to create an extensible mechanism for linked drop downs.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

713 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