Check Character Length of TexBox Control in ASP.NET

TextBox Control TextValue length Check  Programmatically

- In this ASP.Net TextBox Example we will learn How to check the Length of String in TextBox Programmatically.

- Here we take Example of TextBox Control check the Entered value is Equal to 5 or Higher or Less in ASP.Net.
Write below code in Body Tag :-

<body>
    <form id=”form1″ runat=”server”>
    <div>
        <table border=”1″ bordercolor=”#000000″ style=”z-index: 100; left: 288px; position: absolute;
            top: 80px”>
            <tr>
                <td colspan=”3″>
                    <asp:Image ID=”Image1″ runat=”server” ImageUrl=”~/img/newimg.png” /></td>
            </tr>
            <tr>
                <td colspan=”3″ rowspan=”2″ style=”height: 21px; text-align: center”>
                    <table>
                        <tr>
                            <td style=”width: 100px”>
                                Enter Value :</td>
                            <td style=”width: 100px”>
                                <asp:TextBox ID=”txtvalue” runat=”server”></asp:TextBox></td>
                            <td style=”width: 100px”>
                            </td>
                        </tr>
                        <tr>
                            <td style=”width: 100px”>
                            </td>
                            <td style=”width: 100px”>
                                <asp:Button ID=”btnsubmit” runat=”server” OnClick=”btnsubmit_Click” Text=”Submit” /></td>
                            <td style=”width: 100px”>
                            </td>
                        </tr>
                        <tr>
                            <td style=”width: 100px”>
                            </td>
                            <td colspan=”2″ style=”text-align: left”>
                                <asp:Label ID=”Label1″ runat=”server” Font-Bold=”True”></asp:Label></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
            </tr>
        </table>
    
    </div>
    </form>
</body>

TextBox control cannot be Blank

TextBox control cannot be Blank

- If the TextBox Value Blank then Display the Error Message Enter Some Value in TextBox Control.

 

Write Below code in Submit Button Click Event:-

protected void btnsubmit_Click(object sender, EventArgs e)
{
if (txtvalue.Text != “”)
{
if (txtvalue.Text.Length == 5)
{
Label1.Text = “Value is equal to 5 character !”;
Label1.ForeColor = System.Drawing.Color.DarkRed;
}
else if (txtvalue.Text.Length > 5)
{
Label1.Text = “Value is bigger then 5 character !”;
Label1.ForeColor = System.Drawing.Color.DarkGreen;
}
else
{
Label1.Text = “Value is smaller then 5 Character !”;
Label1.ForeColor = System.Drawing.Color.DarkBlue;
}
}
else
{
Label1.Text = “Enter some value !”;
Label1.ForeColor = System.Drawing.Color.Black;
}

}

TextBox control in ASP.Net

TextBox control in ASP.Net

In above figure the TextBox Control Value ‘meera’  length=5 then display message like above.

TextBox control in ASP.NetTextBox control in ASP.Net

TextBox control in ASP.Net

- The Value ‘meeraacademy’ is bigger then 5 character so display message like above in ASP.Net.

TextBox control in ASP.Net

TextBox control in ASP.Net

- In Above ASP.Net TextBox Example we learnt how to check the length of TextBox control value in ASP.Net C#.

- I hope you enjoy this TextBox Example of ASP.Net.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 7 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">