How can I set a minimum amount of characters that should be set to a textbox
?
I'm using Visual Studio 2013, creating a site in aspx.
How can I set a minimum amount of characters that should be set to a textbox
?
I'm using Visual Studio 2013, creating a site in aspx.
It's very simple, if you're using asp:TextBox
just use a asp:RegularExpressionValidator
, getting 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}.*" />
Just make a filter by checking the number of characters in the TextBox.
if(textBox1.TextLenght > 5) //Ou qualquer outro número que você definir
{
FacaOQueTemQueFazer();
}
Add a very basic check in the event where you will validate what is in the TextBox.
Let's take a look at% of your TextBox, and% of it's 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, to use in a simpler way:
if (!TextBox1.Length >= limite) return;