asp.net 验证以确保文本框具有整数值 [英] asp.net validation to make sure textbox has integer values
问题描述
我对文本框进行了必需的验证设置,但我还必须确保它是一个整数.
I have a required validation setup on a textbox, but I also have to make sure it is an integer.
我该怎么做?
推荐答案
如果你只关心字段包含一个整数(即不关心范围),那么添加一个 CompareValidator
与它的 Operator
属性设置为 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" />
如果有特定范围的有效值(可能有),那么您可以使用 RangeValidator
,像这样:
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" />
这些只会验证文本框中是否有文本,因此您需要保留 RequiredFieldValidator
也有.
These will only validate if there is text in the TextBox, so you will need to keep the RequiredFieldValidator
there, too.
作为 @Mahin 说,请确保检查 Page.IsValid
属性在服务器端,否则验证器仅适用于启用了 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屋!