View Full Version : validate length
mini_minh
08-21-2007, 05:11 PM
hiya...i'm trying to make an asp.net 2.0 web page using visual studio 2005..
was wondering how i could validate the length for input in a text box...
the input needs to b 4 characters long...
thanx in advance.
sage45
08-21-2007, 05:43 PM
hiya...i'm trying to make an asp.net 2.0 web page using visual studio 2005..
was wondering how i could validate the length for input in a text box...
the input needs to b 4 characters long...
thanx in advance.Text box or Text area?' For a text area or multiline text box.
const int LENGTH_TEXT = 100;
protected void Page_Load(object sender, EventArgs e)
{
string
lengthFunction = "function isMaxLength(txtBox) {";
lengthFunction += " if(txtBox) { ";
lengthFunction += " return ( txtBox.value.length <=" + LENGTH_TEXT + ");";
lengthFunction += " }";
lengthFunction += "}";
this.txtMyTextBox.Attributes.Add("onkeypress", "return isMaxLength(this);");
ClientScript.RegisterClientScriptBlock(
this.GetType(),
"txtLength",
lengthFunction , true);
}If, however, this is a single line text box, just use the maxLength Property.Public Sub CreateMyPasswordTextBox()
' Create an instance of the TextBox control.
Dim textBox1 As New TextBox()
' Set the maximum length of text in the control to eight.
textBox1.MaxLength = 8
' Assign the asterisk to be the password character.
textBox1.PasswordChar = "*"c
' Change all text entered to be uppercase.
textBox1.CharacterCasing = CharacterCasing.Upper
' Align the text in the center of the TextBox control.
textBox1.TextAlign = HorizontalAlignment.Center
End Sub
HTH,
-saige-
mini_minh
08-21-2007, 05:46 PM
yea i can do maxlength...
but i'm trying to make it so that the user inputs 4 characters
no more no less
like is there a minimumlength property?
SouthwaterDave
08-21-2007, 08:10 PM
You could use a RegularExpressionValidator with a ValidationExpression of ^.{4}$. If you want to ensure that you have 4 non-space characters then use ^\s{4}$ instead.
nikkiH
08-21-2007, 10:26 PM
You could use a RegularExpressionValidator with a ValidationExpression of ^.{4}$. If you want to ensure that you have 4 non-space characters then use ^\s{4}$ instead.
This.
No reason to use page_load or anything for .NET 2.0.
Validators are awesome.
http://www.codeproject.com/aspnet/aspnetvalidation.asp
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.