Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅对文本框中的数字使用RegularExpressionValidator_C#_Asp.net_Visual Studio 2012_Webforms - Fatal编程技术网

C# 仅对文本框中的数字使用RegularExpressionValidator

C# 仅对文本框中的数字使用RegularExpressionValidator,c#,asp.net,visual-studio-2012,webforms,C#,Asp.net,Visual Studio 2012,Webforms,Visual Studio 2012、Asp.net、webforms。 试图控制文本框的输入,仅限数字。我有以下代码: <asp:RegularExpressionValidator id="RegularExpressionValidator1" ControlToValidate="txtAcres" ValidationExpression="^\d+" Display="Sta

Visual Studio 2012、Asp.net、webforms。
试图控制文本框的输入,仅限数字。我有以下代码:

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


但我可以输入任何文本。我缺少什么?

您需要为
EnableClientScript
属性设置
true

 EnableClientScript="true" 
<asp:TextBox runat="server" ID="txtstock" width="50" />
        <asp:RegularExpressionValidator runat="server" ErrorMessage="Numeric Only" ControlToValidate="txtstock"
      ValidationExpression="^[1-9]\d$"></asp:RegularExpressionValidator>
使用EnableClientScript属性指定客户端 已启用验证。验证控件始终执行验证 在服务器上。它们还具有完整的客户端实现 允许DHTML支持的浏览器(如Microsoft Internet Explorer 4.0及更高版本)对客户端执行验证。客户端验证通过检查用户输入来增强验证过程 在将其发送到服务器之前。这允许在服务器上检测错误 在提交表单之前,请与客户联系,避免 服务器端验证所需的信息


首先检查文本框是否为空,然后只检查数字

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

检查文本框是否为空:

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

仅允许数字:

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

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

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

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

祝你成功

尝试:
ValidationExpression=“^\d+$”
通过将EnableClientScript更改为true,我收到错误:WebForms UnobtrusiveValidationMode需要“jquery”的ScriptResourceMapping。请添加名为jquery(区分大小写)的ScriptResourceMapping。如果您使用的是framework 4.5,那么这可能会很有用,在针对.NET 4.5时,默认情况下我们会启用不引人注目的验证。您需要在项目中使用jQuery,并在Global.asax中使用类似的内容才能正确注册jQuery,