问题描述
我一直在做双类型支票在网页上输入字段,但现在我需要让逗号。可以这样使用CompareValidator做或做我需要使用正则表达式验证?
I had been doing a type check for Double for an input field on a web page but now I need to allow commas. Can this be done using a CompareValidator or do I need to use a regex validator?
推荐答案
而不是使用 TYPE =双师型
,请尝试使用 TYPE =货币
。它应该接受的值有和没有逗号,但它不会接受超过2位小数。
Rather than using Type="Double"
, try using Type="Currency"
. It should accept values with and without commas, however it will not accept more than 2 decimal places.
下面是一个例子:
<asp:TextBox runat="server" ID="TextBox1" />
<asp:CompareValidator runat="server" ID="cValidator" ControlToValidate="TextBox1"
Type="Currency" Operator="DataTypeCheck" EnableClientScript="true"
ErrorMessage="Invalid format!" Display="Dynamic" />
另外一个 RegularEx pressionValidator
会的工作,再加上的RequiredFieldValidator
校验空条目(正则表达式验证程序不prevent空条目)。你可以使用的CustomValidator
,但你需要拿出一个JavaScript客户端验证程序,如果你不想在服务器端验证仅仅依靠一个回传。此外,客户端解决方案可能涉及正则表达式,它的更多的工作来全面验证,虽然不是太复杂了。
Otherwise a RegularExpressionValidator
would work, coupled with a RequiredFieldValidator
to validate empty entries (the regex validator doesn't prevent empty entries). You could use a CustomValidator
, but you would need to come up with a client side validation routine in JavaScript if you don't want to rely solely on server side validation with a postback. Also, the client side solution may involve regex and it's more work to validate overall, although not too complicated.
这是用一个例子 RegularEx pressionValidator
:
<asp:TextBox runat="server" ID="TextBox1" />
<asp:RequiredFieldValidator runat="server" ID="rfValidator" Display="Dynamic"
ControlToValidate="TextBox1" ErrorMessage="Required!" />
<asp:RegularExpressionValidator ID="reValidator" runat="server"
ControlToValidate="TextBox1"
EnableClientScript="True"
ErrorMessage="Invalid Format!"
Display="Dynamic"
ValidationExpression="(\d{1,3}(,\d{3})*\.\d{2})|(\d+(\.\d{2})?)" />
这篇关于可能得到CompareValidator来接受用逗号的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!