Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to restrict the user that he cant enter more than 6 characters in a text box.As a user start entering in the text box and reach to the max limit and try entering 7th character, the text box will not allow him to do so and alert him that max size reached.How i achieve this?

I had added JavaScript function in my code to do so but not getting that on which event i call this function , currently i am calling on on-blur but i m able to type more than 9 character in the text box and after that when i m clicking on a button then i m getting alert message.i want that while typing in the text box itself if max limit reached the user get prompted.
Posted

Try using the MaxLength property[^].
 
Share this answer
 
Comments
StianSandberg 16-Jul-12 2:14am    
"i want that while typing in the text box itself if max limit reached the user get prompted". This will not prompt.
Sandeep Mewara 16-Jul-12 3:27am    
5! Answer to what was asked originally.
Use Textbox property: MSDN: Textbox.MaxLength[^]
 
Share this answer
 
Comments
Abhinav S 16-Jul-12 2:11am    
This was my answer exactly. 5 though. ;)
Sandeep Mewara 16-Jul-12 3:26am    
Yeah. Ditto. 5 to you too. :)
StianSandberg 16-Jul-12 2:13am    
"i want that while typing in the text box itself if max limit reached the user get prompted". This will not prompt.
Sandeep Mewara 16-Jul-12 3:27am    
You did not mention anyting about prompt in your original question.
Based on followup question: "i want that while typing in the text box itself if max limit reached the user get prompted". This will not prompt.
Steps:
1. Define a onkeyup for the textbox
2. Tie up a Javascript funtion to onKeyup
3. check the length and prompt.

Sample:
HTML:
HTML
<input type"text" id="mytb" onkeyup="CheckLength(this.value);"/>

JS Code:
JavaScript
function CheckLength(currentValue)
{
   if(currentValue.length > 6)
      alert("Only 6 letters are allowed, please follow the rule!");
}


You can modify the implementation as per your need.
 
Share this answer
 
Comments
Rahul Rajat Singh 16-Jul-12 8:04am    
good answer. +5. also we need to call this function from onkeypress and return false after the alert to make it work properly.
use to attributes:
maxlength="6"
This is pure html and will in most browsers work, but will not give any alerts!

HTML
<input type="text"  onkeydown="return testLength()" id="mytextbox" />


JavaScript
function testLength(){
   var e = document.getElementById('mytextbox');
   if(e.value.length>6)
   {
      alert('no no no');
      // Set value back to the first 6 characters 
      e.value = e.value.substring(0, 6);
   }
   return true;
}
 
Share this answer
 
v5
make a function of JScript


function MaxLimit()
{
    var e = document.getElementById('txtmax');
    if(e.value.Lenght>6)
    {
      alert('Your Msg');
    }
}
 
Share this answer
 

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