Click here to Skip to main content
15,910,886 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a text box i want to restrict that value for max value using key events

I try with the below code and its work fine
JavaScript
function AdjustKeyPress(e, object, value) {
        var KeyID = (window.event) ? event.keyCode : e.which;

        if ((KeyID >= 65 && KeyID <= 90) || (KeyID >= 97 && KeyID <= 122) || (KeyID >= 33 && KeyID <= 47) ||
                    (KeyID >= 58 && KeyID <= 64) || (KeyID >= 91 && KeyID <= 96) || (KeyID >= 123 && KeyID <= 126)) {
            return false;
        }

        if (KeyID == 32) {
            return false;
        }
        var recentChar = String.fromCharCode(e.which);
        if (object.value + recentChar > value) {
            return false;
        }
    }

Its work fine when we insert or press key at the end of text box. but when i press a key in between value then its wrong..
C#
<asp:TextBox ID="txtColorsize" MaxLength="30" onkeypress="javascript:return AdjustKeyPress(event,this,182)" runat="server">

if i change last '2' value its work fine,but If i replace '8' with '9' its becomes '129' instead of '192'

Please help me to find the solution
Thanks in advance
Posted

Bad idea! Better validate the input later, otherwise your validation per key press will create a great confusion in the user.

Here is why. Imagine your max value is 900. The user enters 799 (valid) and realized that the valid value would be 789 (still valid). The user can delete on of '9' and enter '8'. It would keep input valid all the time. Alternatively, the user can enter '8' after already entered '7' in hope to delete extra '9' on next step. To the user surprise, an error message will be shown. The user will have to click "OK" (or something) and delete extra '9' after that, if it's even possible, probably mumbling some 4-letter words at your address in the act.

Do you want it? Better validate the input later, at the moment it is used.

At the same time, filtering out of unwanted characters on input would be quite useful. You can allow only digits, for example. Don't forget to allow the character #8, backspace — it is treated like a character, probably by historical reasons.
For example of such code, see how to restrict users to put the numbers in range of -4713 and +9999 in javascript?[^].

—SA
 
Share this answer
 
v2
Firstly, use the "onkeyup" instead of "onkeypress" which is more accurate! and then in that event calculate the length of the textbox value like so:

HTML
<input id="txtInput" value="" onkeyup="AdjustKeyPress(this.value);" />

JavaScript
function AdjustKeyPress( value )
{
  //change max value
  var maxValue = 200;
  if( value.length> maxValue )
  {
      //show error
  }
  else
  {
     //proceed
  }
}
 
Share this answer
 
Comments
AditSheth 12-Sep-11 3:19am    
Hello
I don't want to validate Value length. I want to restrict value.
first understood question and then post solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900