Web 控件 - 验证

发布于 2024-09-18 22:41:39 字数 223 浏览 2 评论 0原文

我正在使用 ASP.NET 4。

我想检查插入文本框中的字符串的长度。例如:输入名称不超过 255 个字符。

我的问题:

  • 我可以在 ASP.NET 中使用哪些 Web 控件进行验证?
  • 用于验证的 Web 控件同时验证客户端和服务器端?我的意思是,如果没有启用 Java 脚本,控件是否能够验证(在服务器端)?

感谢您抽出时间

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2024-09-25 22:41:39

为了方便用户,您可以组合文本框的 MaxLength 属性,以及用于客户端和服务器端验证的 RegularExpressionValidator

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

如果客户端验证失败,则会阻止回发。如果 javascript 被禁用,或者您的客户端是攻击者,验证仍然会在服务器端进行。

要在服务器端查询页面以查看验证是否成功,请检查页面上的 IsValid 属性,并采取相应的操作。

您可以查看此资源以获取 IsValid 用法的示例。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文