TextBox验证问题 [英] TextBox validation problem

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

问题描述

在我的一个文本框中,我想比较整数条目。我也使用了比较验证器和范围表达式验证器。在文本框中,如果我使用空格

键输入空格,则两个验证器都不会抛出错误。它正在接受!请在我的代码中建议可能的更正,或者在C#中使用空格键ascii代码的值是否存在任何可能性。以下是我的比较验证器和范围表达式验证器的代码。



In one of my textbox I wanted to compare for integer entry. I used Compare validator and Range expression validator as well. In the textbox, at the beginning if I enter a space using space
key, both the validators are not throwing error. It is accepting! Please advice for possible correction in my code or is there any possibility in C# by using the value of space bar ascii code. Following is my code for Compare validator and Range expression validator.

<asp:CompareValidator ID="CompareValidator1" runat="server"
            Operator="DataTypeCheck"
            Type="Integer"
            ControlToValidate="TextBox14"
            Text="Text must be an integer." ForeColor="Red"
            style="z-index: 1; left: 90px; top: 22px; position: absolute; " />













<asp:RegularExpressionValidator ID="RegularExpressionValidator31"

            runat="server" ControlToValidate="TextBox14" ErrorMessage="Enter an integer"

            Font-Size="X-Small"

            style="z-index: 1; left: 90px; top: 22px; position: absolute"

            ValidationExpression="[0.00-9.00]{1,7}"></asp:RegularExpressionValidator>

推荐答案

验证通过leadin完全没问题g或尾随空格。如果您尝试在带有前导和/或尾随空格的数字字符串上运行TryParse或Convert,那么同样的事情就可以了。数值仍然有效,只需要修剪。



[更新]

当你回发你的页面而你想要使用文本框中的值作为数值,你可以这样做。

It is perfectly fine that the validation passes with leading or trailing spaces. The same thing would be ok if you attempted to run a TryParse or Convert on a numerical string with leading and/or trailing spaces. The numerical value is still valid, it just needs to be trimmed.

[Update]
When you postback your page and you want to work with the value in the Textbox as a numerical value, you would do something like this.
// In the case you want an integer value.
int value;
if( Int32.TryParse( YourTextBox.Text, out value ) == false )
{
   // If the TryParse call fails to convert the value to an integer, 
   // set a default value here.
}

// In the case you want an double value.
double value;
if( Double.TryParse( YourTextBox.Text, out value ) == false )
{
   // If the TryParse call fails to convert the value to an double, 
   // set a default value here.
}


这篇关于TextBox验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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