Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NET
Dear fellow,
How can I fix character length in textbox? Say i want to restrict to 3 three characters. It should not only to enter less than or more than three characters. And secondly, I want to allow only text in the textbox. Please help me out.
Posted 18 Jun '10 - 22:11


7 solutions

1.To restrict maxlength of textbox with three character, write
<asp:TextBox ID="TextBox1" runat="server" MaxLength="3"></asp:TextBox>
 
2.To match exactly three character in textbox add following javascript function
 
function checkLength(obj) {
            if (obj.value.length != 3) {
                alert("Should be exactly three char.");
                obj.focus();
            }
        }
 
And call the function in onblur event
TextBox1.Attributes.Add("onblur", "checkLength(this)");
 
3.To allow only text in textbox add following javascript function
function handleKeydown(event) {
            keyEntry = event.keyCode || event.which;
            if (
                ((keyEntry >= '65') && (keyEntry <= '90')) ||
                ((keyEntry >= '97') && (keyEntry <= '122')) ||
                (keyEntry == '46') ||
                (keyEntry == '32') ||
                keyEntry == '45' ||
                keyEntry == '46' ||
                keyEntry == '8' ||
                keyEntry == '35' ||
                keyEntry == '36'||
                keyEntry == '9')
                return true;
            else
                return false;
        }
 
And add onkeydown & onpaste handler in code behind
TextBox1.Attributes.Add("onkeydown", "return handleKeydown(event)");
TextBox1.Attributes.Add("onpaste", "return handleKeydown(event)");
 

Hope this will help you.
  Permalink  
touseef4pk wrote:
Say i want to restrict to 3 three characters. It should not only to enter less than or more than three characters.

 
There are many ways:
1) Use Javascript - Write code onblur of the textbox to check if the length is equal to 3 or not.
<asp:TextBox ID="TextBox1" runat="server" onblur="javascript:validate();"></asp:TextBox>
function validate()
    {
        if(document.getElementById("TextBox1").value.length != 3)
        {
            alert("text lenghth must be three");
            document.getElementById("TextBox1").select();
            document.getElementById("TextBox1").focus();
            return false;
        }
        else
        {
            return true;
        }
    }
 
2) Use RegularExpressionValidator. You need to write a regular expression which checks the value entered is 3 characters.
 
[Edit]
touseef4pk wrote:
Please tell me how can i write reqular expression to limit characters upto three characters in textbox

Try this: \b[a-zA-Z]{3}\b

[/Edit]

 
touseef4pk wrote:
I want to allow only text in the textbox.

 
Text means alphanumeric which is the default for a textbox.
I guess you don't mean that. Please clarify.
  Permalink  
Thanks for reply. Please tell me how can i write reqular expression to limit characters upto three characters in textbox. Actualy I mean to say that the input shoule be alphabetical only like "The". It should not accept numeric values. Please help me to resolve this conflict.
  Permalink  
Comments
Ankur\m/ - 21 Jun '10 - 2:13
You should use comment to discuss with a user. I didn't get a notification for your answer and chances was very low that I would have seen your question again. You see you will get a notification for the comment. Similarly I would have been notified if you commented my answer. Try this regular expression: [a-zA-Z]{3}  (Add a backslash b at the start and end. I don't know why this is not displayed in the comment or wait I am adding it to my previous answer). And yes if an answer, answers your question completely, you should choose "Accept Answer" and you may also vote high for it. Cheers!
For an example on a implenting a validator, see here.
  Permalink  
You can add javascript regex validation.
 
Try "^[a-zA-Z]{3}$" as regular expression for your text box.
This regex will allow only 3 alphabet character.
  Permalink  
Dear.
Please set maxLength properties of Textbox. if you set this 3 then it will not allow to enter more than three characters.
 
For allowing only text, use AjaxControlToolkit. you can download from microsoft download.
 
In ajaxcontrolToolkit, you have to use FilterTextBox. You have to defined the type as 'Integer','Text'.....
 
check this link
 
http://www.asp.net/(S(fu2l2uzphr2u3u45q2dnez55))/ajax/AjaxControlToolkit/Samples/[^]
 
and click on FilteredTextBox
 
Hope it will help you.
 
Thanks
 
Lalit Kumar
  Permalink  
Comments
Ankur\m/ - 30 Jun '10 - 6:52
Reason for my vote of 2 The OP needs EXACTLY 3 characters.
For 3 Character length use
 Maxlength="3"
property in textbox
and for character only i think
AJAX:FilterTextboxExtender
control will work
  Permalink  
Comments
Ankur\m/ - 30 Jun '10 - 6:51
Reason for my vote of 1 The OP needs EXACTLY 3 characters. Moreover you have put same answer as above poster(Lalit)

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 223
1 Ron Beyer 220
2 Aarti Meswania 200
3 Mahesh Bailwal 175
4 Rohan Leuva 170
0 Sergey Alexandrovich Kryukov 8,553
1 OriginalGriff 6,899
2 CPallini 3,648
3 Rohan Leuva 2,963
4 Maciej Los 2,308


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 6 Aug 2010
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid