Hi,

Please anyone help me out............

I want the users to enter the data in the textbox of the minimum length of "10" and maximum length can be "ANY"(or above "10").

Thanks in advance...........................

Dani AI

Generated

is right that you should always validate on the server, but you can still give users instant feedback. For a simple "at least 10 chars, no max" rule, combine a required check with a regex that anchors the whole input. Anchors avoid edge cases and make intent clear.

<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator runat="server"
    ControlToValidate="TextBox1"
    ErrorMessage="This field is required." Display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
    ControlToValidate="TextBox1"
    ValidationExpression="^.{10,}$"
    ErrorMessage="Enter at least 10 characters." Display="Dynamic" />

If you prefer ’s custom approach, make sure you trim and allow exactly 10 too:

e.IsValid = (e.Value ?? string.Empty).Trim().Length >= 10;

Client-side hint (optional, not a security barrier): add the HTML5 attribute so modern browsers show native errors, but still keep the server rule.

TextBox1.Attributes["minlength"] = "10";

Notes:

  • Do not set MaxLength; leave it unset to allow any length.
  • Use ValidationGroup on your validators and submit button if you have multiple forms on the page.
  • Ensure the submit button triggers validation (CausesValidation="true").

For follow-ups in this thread:

  • : If you only want Indian mobile numbers, a focused pattern is simpler and safer than a very broad one:
    ^(?:\+91[-\s]?|0)?[6-9]\d{9}$
    Store phone numbers as strings, not numeric, so you keep leading zeros and formatting.
  • : To enforce a numeric value of at least 500, use a CompareValidator:
<asp:CompareValidator runat="server"
    ControlToValidate="Amount"
    Operator="GreaterThanEqual"
    Type="Integer"
    ValueToCompare="500"
    ErrorMessage="Enter 500 or more." />

Recommended Answers

All 9 Replies

Why not just validate on the postback and come back with an error message if it is <10 characters? I can't think of any website that enforces a minimum length validation browser side.

You can use a RegularExpression validation for this purpose.

<asp:TextBox ID="TextBox1" runat="server"  />
<asp:RegularExpressionValidator ID="regExTextBox1" runat="server" 
   ControlToValidate="TextBox1"
   ErrorMessage="Minimum password length is 10"
   ValidationExpression=".{10}.*" />
commented: or that +15

or you could use a custom validator:

this part in your asp page

<asp:TextBox id="txtTextBox" runat="server"/>
<asp:CustomValidator id="cvLengthValidator" runat="server" OnServerValidate="cvLengthValidator_OnServerValidate" ErrorMessage="Minimum Length is 10" ControlToValidate="txtTextBox"/>

in your code behind

protected void cvLengthValidator_OnServerValidate(object sender, ServerValidateEventArgs e)
{
       e.IsValid = e.Value.ToString().Length > 10;
}

You can use a RegularExpression validation for this purpose.

<asp:TextBox ID="TextBox1" runat="server"  />
<asp:RegularExpressionValidator ID="regExTextBox1" runat="server" 
   ControlToValidate="TextBox1"
   ErrorMessage="Minimum password length is 10"
   ValidationExpression=".{10}.*" />

Hi, Thanks for solution.....

Hi kailasgorane,

Please mark this thread as solved if your question is answered.

Regards
Ramesh. S

In Textbox i want to enter the India phone no The textbox take 10 digits only not more than one number

To resolve this issues.go for custom validator along with regexpression for phone number validating.To get the regular expression for phone number

Expression : ^((+)?(\d{2}[-])?(\d{10}){1})?(\d{11}){0,1}?$
Description : India phone number, accept with optional +91 national code and 0 for land and mobile number prefix . Allows optional - after national code

for more info Click Here

hope this will help you.

The suggetion of Ramesh for using Reguler Expression is really working . Thanks dude!!!

Thanks Ramesh.. It worked for me.

I've one more question can I check for value of the textbox like if value should be atleast 500 or more??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.