RegularExpressionValidator抛出错误 [英] RegularExpressionValidator throwing error

查看:119
本文介绍了RegularExpressionValidator抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文本框的网络表单。我为每个文本框都有两个不同的验证器。第一个验证器是范围验证器,第二个验证器是regularrexpressionvalidator。我将REV ValidationExpression设置为^ [0-9]。当用户测试文本框以查看将发生什么时,会出现此错误:

输入字符串的格式不正确。





文本框上有一个文本框文本更改,并带有计算结果。有没有办法让文本框只允许数字而不进行任何计算,除非文本框中有数字?



 <   asp:TextBox     ID   =  TextBoxFTUG    runat   = 服务器   宽度  =  180px    AutoPostBack   =  True    ontextchanged   =  TextBoxFTUG_TextChanged    

> 0 < / asp:TextBox
>



 <   asp:RegularExpressionValidator     ID   =  RegularExpressionValidator1    runat   =  server    

< span class =code-attribute> ControlToValidate = TextBoxFTUG 显示 = 动态 ErrorMessage = 仅限数字

style = 颜色:#FF0000; font-weight:700 ValidationExpression = ^ [0-9] > < / asp:RegularExpressionValidator >



 <   asp:RangeValidator     ID   =  RangeValidatorLYFTUG    runat   =   server    

ControlToValidate = TextBoxFTUG CssClass = style40

< span class =code-attribute> ErrorMessage = 20%差异。请验证数字 ForeColor = 红色

类型 = 整数 已启用 = True 显示 = 动态 > < / asp:RangeValidator >



这是我收到错误的地方:

输入字符串不正确格式。 
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.FormatException:输入字符串的格式不正确。

来源错误:


第204行:protected void TextBoxFTUG_TextChanged(object sender,EventArgs e)
第205行:{
第206行:int w = Convert.ToInt32(TextBoxFTUG.Text.Replace(,,));
第207行:int x = Convert.ToInt32(TextBoxFTG.Text.Replace(,,));
第208行:int c = Convert.ToInt32(TextBoxTHUGDR.Text.Replace(,,));


源文件:C:\Users\khopkins \Documents\Visual Studio 2010 \Projects\SACSCOCLogin1.1\SACSCOCLogin1.1\FTEEnrollmentInformation.aspx。 cs行:206

解决方案

我只是猜测你可能想要 ^ \d +


从你的问题不清楚你到底哪里得到错误,但有一件事是肯定的 - 你的正则表达式没有好...

[0-9] 仅适用于单个数字。如果您想要超过该用户 [0-9] + \d + d 用于数字),忘记 ^ (或


I have a web form that has textboxes on it. I have two different validators for each textbox. The first validator is a range validator and the second validator is a regularexpressionvalidator. I have the REV ValidationExpression set to ^[0-9]. When the user tests the textbox to see what will happen this error comes up:

Input string was not in a correct format.



The textbox does have a textbox text change on it with calculations. Is there a way to get the textbox to only allow numbers and not do any calculations unless there is a number in the textbox?

<asp:TextBox ID="TextBoxFTUG" runat="server" Width="180px" AutoPostBack="True" ontextchanged="TextBoxFTUG_TextChanged" 

                    >0</asp:TextBox>


<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 

                    ControlToValidate="TextBoxFTUG" Display="Dynamic" ErrorMessage="Numbers Only" 

                    style="color: #FF0000; font-weight: 700" ValidationExpression="^[0-9]"></asp:RegularExpressionValidator>


<asp:RangeValidator ID="RangeValidatorLYFTUG" runat="server" 

                    ControlToValidate="TextBoxFTUG" CssClass="style40" 

                    ErrorMessage="20% Difference. Please verify numbers" ForeColor="Red" 

                    Type="Integer" Enabled="True" Display="Dynamic"></asp:RangeValidator>


Here is where I get my error:

 Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 204:    protected void TextBoxFTUG_TextChanged(object sender, EventArgs e)
Line 205:    {
Line 206:        int w = Convert.ToInt32(TextBoxFTUG.Text.Replace(",", ""));
Line 207:        int x = Convert.ToInt32(TextBoxFTG.Text.Replace(",", ""));
Line 208:        int c = Convert.ToInt32(TextBoxTHUGDR.Text.Replace(",", ""));


Source File: C:\Users\khopkins\Documents\Visual Studio 2010\Projects\SACSCOCLogin1.1\SACSCOCLogin1.1\FTEEnrollmentInformation.aspx.cs    Line: 206 

解决方案

I'm just guessing that you probably want ^\d+


.


It is not clear from your question where you exactly got the error, but one thing is sure - your regex not good...
[0-9] is good only for a single digit. If you want more than that user [0-9]+ or \d+ (d is for digit), and forgot the ^ (or


这篇关于RegularExpressionValidator抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆