ASP.NET RangeValidator控件存在异常 [英] ASP.NET RangeValidator Behaving Strangely
问题描述
有的类型属性被设置为串和它的最小值= 0和最大值= 100
It's type attribute is set to string and it's min value = 0 and the max value = 100.
我想,以确保输入的数据将不超过在数据库中定义的列长度。
I want to insure that the data entered won't exceed the column length defined in the database.
现在,当我测试了一下,始终显示错误消息,即使我输入一个字母或两个!
Now when I test it, it always display the error message even if I entered one letter or two!
推荐答案
您应该使用RegularEx pressionValidator这一点。 RangeValidators不是为字符串长度。设置ValidationEx pression喜欢的东西?
,也可以将其缩小到像 \\ s?
或 \\ w?
或东西,如果你想要的。
You should use a RegularExpressionValidator for this. RangeValidators aren't for string lengths. Set the ValidationExpression to something like .?
, or you can narrow it to something like \S?
or \w?
or something if you want.
另外,如果你有一个文本框,这不是在多行模式工作(而且也没有理由如果Max是一个字符,它会),你可以只设置 MAXLENGTH =1
上的控制,你会根本不需要一个验证器。
Also, if you're working with a TextBox that's not in MultiLine mode (and there's no reason it would be if the max is one character) you can just set MaxLength="1"
on the control and you won't need a validator at all.
编辑::如果你想指定一个最大长度大于一对多行文本框,你可以使用一个RegularEx pressionValidator如上所述,但设置 ValidationEx pression =
为100的在正则表达式量词的更多信息请点击这里。
If you do want to specify a max length greater than one on a multiline TextBox, you can use a RegularExpressionValidator as described above, but set ValidationExpression=".{0,100}"
for a maximum length of 100. More information on regex quantifiers is available here.
如果你有一个单行文本框,只需设置它的maxlength属性到所需的最大值,它不会允许任何东西都比较长;无需验证。
If you have a single-line TextBox, just set its MaxLength attribute to the desired maximum, and it won't allow anything longer than that; no validator required.
如果你想做的任何事情,涉及长度为用户类型吧,那是更加复杂的实时检测和你需要的JavaScript。还应该仍然验证输入服务器侧,因为用JavaScript任何用户关闭将能够绕过客户端验证
If you want to do anything involving real-time detection of the length as the user types it, that's more complicated and you'll need JavaScript. You should also still validate the input server-side, because any user with JavaScript turned off will be able to bypass client-side validation.
这篇关于ASP.NET RangeValidator控件存在异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!