C# 仅在文本框中对数字使用正则表达式验证器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16306002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:42:56  来源:igfitidea点击:

Using RegularExpressionValidator for numbers only in textbox

c#asp.netvisual-studio-2012webforms

提问by KFP

Visual Studio 2012, Asp.net, webforms.
Trying to control input into textbox, numbers only. I have the following code:

Visual Studio 2012、Asp.net、网络表单。
试图控制输入到文本框,只有数字。我有以下代码:

<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                 ControlToValidate="txtAcres"
                 ValidationExpression="^\d+"
                 Display="Static"
                 ErrorMessage="Only Numbers"
                 EnableClientScript="False" 
                 runat="server"></asp:RegularExpressionValidator>

but i am allowed to enter any text. What am I missing?

但我可以输入任何文本。我错过了什么?

回答by Adil

You need to set truefor EnableClientScriptproperty.

您需要设置trueEnableClientScript属性。

 EnableClientScript="true" 

Use the EnableClientScript property to specify whether client-side validation is enabled. Validation controls always perform validation on the server. They also have complete client-side implementation that allows DHTML-supported browsers (such as Microsoft Internet Explorer 4.0 and later) to perform validation on the client. Client-side validation enhances the validation process by checking user input before it is sent to the server. This allows errors to be detected on the client before the form is submitted, avoiding the round trip of information necessary for server-side validation, Reference

使用 EnableClientScript 属性指定是否启用客户端验证。验证控件始终在服务器上执行验证。它们还具有完整的客户端实现,允许支持 DHTML 的浏览器(例如 Microsoft Internet Explorer 4.0 和更高版本)在客户端上执行验证。客户端验证通过在将用户输入发送到服务器之前检查用户输入来增强验证过程。这允许在提交表单之前在客户端检测错误,避免服务器端验证所需的信息往返,参考

回答by Apollo

This checks first if textbox is blank and then it checks for numbers only.

这首先检查文本框是否为空白,然后仅检查数字。

<asp:TextBox ID="tbAccount" runat="server"></asp:TextBox>

Checks if textbox is blank:

检查文本框是否为空:

<asp:RequiredFieldValidator ID="RequiredFieldValidatorAccount" runat="server" ErrorMessage="*Required" ControlToValidate="tbAccount" ForeColor="Red"></asp:RequiredFieldValidator>

Allows only numbers:

只允许数字:

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="tbAccount" ErrorMessage="Please Enter Only Numbers" ForeColor="Red" ValidationExpression="^\d+$"></asp:RegularExpressionValidator>

回答by TechnicalKalsa

You can use this code on ASPXPage. Use ^[1-9]\d$in ValidationExpression property.

您可以在ASPX页面上使用此代码。^[1-9]\d$在 ValidationExpression 属性中使用。

<asp:TextBox runat="server" ID="txtstock" width="50" />
        <asp:RegularExpressionValidator runat="server" ErrorMessage="Numeric Only" ControlToValidate="txtstock"
      ValidationExpression="^[1-9]\d$"></asp:RegularExpressionValidator>

回答by Tran Anh Hien

You can use ^(0|[1-9]\d*)$Good Sucessful !

您可以使用^(0|[1-9]\d*)$Good Sucessful !