Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 23:11pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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/ at 21-Jun-10 2:13am
   
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!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

For an example on a implenting a validator, see here.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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/ at 30-Jun-10 6:52am
   
Reason for my vote of 2
The OP needs EXACTLY 3 characters.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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/ at 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)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 11

<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>
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 490
1 Maciej Los 299
2 BillWoodruff 174
3 /\jmot 170
4 Suraj Sahoo | Coding Passion 150
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 19 Sep 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100