Set minimum number of characters in Textbox

Asked

Viewed 2,379 times

0

How can I define a minimum amount of characters that should be put into a textbox?

I’m using Visual Studio 2013, creating an aspx site.

  • You can check the amount of characters in the textbox Textchanged event.

  • what exactly are you using? Asp-net MVC? which version?

  • You can try using input: <input id="test" runat="server" type="text" Pattern=". {3,}" title="3 characters minimum" /> http://stackoverflow.com/questions/10281962/is-there-a-min-validation-attributin-html5

3 answers

1

It’s very simple if you’re using the asp:TextBox just use a asp:RegularExpressionValidator, being as follows

<asp:TextBox runat="server" ID="TextBox1" MaxLength="100"></asp:TextBox>
<asp:RegularExpressionValidator ID="valPassword"
        runat="server"
        ControlToValidate="TextBox1"
        ErrorMessage="Tamanho mínimo pro TextBox é 5"
        ValidationExpression=".{5}.*" />

0

Only make a filter by checking the amount of characters in the Textbox.

if(textBox1.TextLenght > 5) //Ou qualquer outro número que você definir
{
    FacaOQueTemQueFazer();
}

0

Add a very basic check in the event where you will validate what is in the Textbox.

Let’s consider TextBox1 its Textbox, and limite the limit (obviously), and consider this method a button, or whatever you want to do with it.

void Validando(Object sender, EventArgs args) {
    if (TextBox1.Length >= limite) {
         // OK, passou do limite mínimo
    } else {
         // não passou
    }
}

Or, use in a simpler way:

if (!TextBox1.Length >= limite) return;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.