Click here to Skip to main content
15,886,781 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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
Updated 30-Jan-20 17:19pm

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

JavaScript
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
JavaScript
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.
 
Share this answer
 
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.
XML
<asp:TextBox ID="TextBox1" runat="server" onblur="javascript:validate();"></asp:TextBox>

JavaScript
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.
 
Share this answer
 
v2
For an example on a implenting a validator, see here.
 
Share this answer
 
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.
 
Share this answer
 
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
 
Share this answer
 
Comments
Ankur\m/ 30-Jun-10 6:52am    
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
 
Share this answer
 
Comments
Ankur\m/ 30-Jun-10 6:51am    
Reason for my vote of 1
The OP needs EXACTLY 3 characters. Moreover you have put same answer as above poster(Lalit)
XML
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
      runat="server" ControlToValidate="txtAgendaDesc"
      ErrorMessage="Please enter maximum 1500 charachters." Text="*" Display="None"
      ValidationExpression="^[a-zA-Z.]{0,1500}$" ValidationGroup="AddAgenda">
</asp:RegularExpressionValidator>
 
Share this answer
 
v2
Comments
Cynetha 23-Jan-15 10:06am    
hey, I am new and using telerik tools. I have a radmasked textbox for phone numbers. There is already a maxLength property i have used. I need to make sure the user enters in the minimum numbers. Now do I something similar to the javascripts solutions already provided or is there something else i have to do since its telerik tools.

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