asp.net验证,以确保文本具有整数值 [英] asp.net validation to make sure textbox has integer values
问题描述
我有一个文本框,一个需要验证设置,但我必须确保它是一个整数也。
I have a required validation setup on a textbox, but I have to make sure it is an integer also.
我怎么能这样做呢?
推荐答案
如果所有你关心的是该字段包含一个整数(即,不涉及一个范围),然后添加一个<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.comparevalidator.aspx\"><$c$c>CompareValidator$c$c>与它的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.comparevalidator.operator.aspx\"><$c$c>Operator$c$c>属性设置为 DataTypeCheck
:
If all that you are concerned about is that the field contains an integer (i.e., not concerned with a range), then add a CompareValidator
with it's Operator
property set to DataTypeCheck
:
<asp:CompareValidator runat="server" Operator="DataTypeCheck" Type="Integer"
ControlToValidate="ValueTextBox" ErrorMessage="Value must be a whole number" />
如果有是有效的(有可能是)值的特定范围,那么你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.rangevalidator.aspx\"><$c$c>RangeValidator$c$c>,像这样:
If there is a specific range of values that are valid (there probably are), then you can use a RangeValidator
, like so:
<asp:RangeValidator runat="server" Type="Integer"
MinimumValue="0" MaximumValue="400" ControlToValidate="ValueTextBox"
ErrorMessage="Value must be a whole number between 0 and 400" />
如果在文本框中的文字,这些只会验证,所以你需要保持<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx\"><$c$c>RequiredFieldValidator$c$c>在那里。
作为<一个href=\"http://stackoverflow.com/questions/1427629/asp-net-validation-to-make-sure-textbox-has-integer-values/1427670#1427670\">@Mahin说,请务必检查的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.page.isvalid.aspx\"><$c$c>Page.IsValid$c$c>在服务器端属性,否则验证程序仅适用于JavaScript的用户启用。
As @Mahin said, make sure you check the Page.IsValid
property on the server side, otherwise the validator only works for users with JavaScript enabled.
这篇关于asp.net验证,以确保文本具有整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!