views:

25

answers:

1

Hi, I am using ASP.NET 4.

I would like check the length for a string inserted in a TextBox. Ex: Input Name no more than 255 characters.

My questions:

  • Which Web Controls for Validation I can use in ASP.NET?
  • Web Controls for Validation validate both Client side and server side? I mean without Java script enabled the Control is able to validate (on server side)?

Thanks for your time

+1  A: 

You can combine the MaxLength property of the text box for user convenience, and the RegularExpressionValidator for client and server-side validation.

<asp:TextBox ID="textbox" runat="server" MaxLength="255" />
<asp:RegularExpressionValidator ID="regtext" runat="server"
    ControlToValidate="textbox"
    ValidationExpression="^.{0,255}$" />

If client-side validation fails, a postback is prevented. If javascript is disabled, or your client is an attacker, validation still occurs on the server side.

To query the page on the server-side to see if validation succeeded, check the IsValid property on the page, and take action accordingly.

You can check out this resource for an example of IsValid usage.

http://msdn.microsoft.com/en-us/library/system.web.ui.page.isvalid.aspx

kbrimington
HI, thanks for your message, could you please send me an complete example with IsValid property? I am really a beginner. thanks for your time.
GIbboK
thanks for your last link :-)
GIbboK