[英]Server-side validation in ASP.NET 2.0
My application is in ASP.NET 2.0 with C#. 我的应用程序在带有C#的ASP.NET 2.0中。 I have a regular expression validator with the regular expression
^[0-9]*(\\\\,)?[0-9]?[0-9]?$
, now my client don't want this validation at client side but on button click ie Server Side. 我有一个带有正则表达式
^[0-9]*(\\\\,)?[0-9]?[0-9]?$
的正则表达式验证器,现在我的客户端不希望在客户端进行此验证,但是在按钮上单击即服务器端。
EX: I have to check the value of txtPrice
textbox 例如:我必须检查
txtPrice
文本框的值
Please let me know how can I put this regular expression validation on server side. 请让我知道如何将该正则表达式验证放在服务器端。
Thanks in advance. 提前致谢。
You can use a CustomValidator which can link to a server side event: 您可以使用CustomValidator来链接到服务器端事件:
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator" OnServerValidate="CustomValidator1_Validate"></asp:CustomValidator>
Then server side you can validate input 然后在服务器端您可以验证输入
protected void CustomValidator1_Validate (object source, ServerValidateEventArgs argss)
{}
Remember to wrap your submit button click with 请记住将您的提交按钮包装为
if(IsValid) {}
To ensure all validators are respected 确保尊重所有验证者
尝试将EnableClientScript="false"
添加到验证器。
The control will validate on the server side always, regardless of whether you also enable client-side validation. 该控件将始终在服务器端进行验证,无论您是否还启用了客户端验证。 But you must then remember to check the value of
Page.IsValid
before accepting the postback... 但是您必须记住在接受回
Page.IsValid
之前检查Page.IsValid
的值...
As has already been said, you can turn off client-side validation with an attribute. 如前所述,您可以使用属性关闭客户端验证。
Client-side validation using server-side controls based on ValidatorBase takes place only on PostBack
ie on any server-side button/linkbutton click. 使用基于ValidatorBase的服务器端控件的客户端验证仅在
PostBack
即在任何服务器端按钮/链接按钮单击上进行。
So you can use RegularExpressionValidator : 因此,您可以使用RegularExpressionValidator :
<asp:TextBox runat="server" ID="txtPrice" />
<asp:RegularExpressionValidator runat="server" ControlToValidate="txtPrice" ValidationExpression="^[0-9]*(\\,)?[0-9]?[0-9]?$" ErrorMessage="Input is incorrect" />
Also you can use CustomValidator : 您也可以使用CustomValidator :
<asp:TextBox runat="server" ID="txtPrice" />
<asp:CustomValidator runat="server" ControlToValidate="txtPrice" ErrorMessage="Input is incorrect" OnServerValidate="CustomValidator1_ServerValidate" />
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
// use e.Value to validate and set e.IsValid
// it's different depending on control to validate.
// for custom controls you can set it using ValidationPropertyAttribute
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.