验证货币输入的最佳方式? [英] Best way to validate currency input?

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

问题描述

我已经创建了下面,我想将允许以下列形式输入文本框和CompareValidator:


  • 5

  • 5.00

  • $ 5.00

不幸的是它不允许在它的美元符号的版本。什么是做一个类型对证的货币,如果你不允许美元符号的意义呢?有没有办法让这个符号?

 < ASP:文本框ID =tb_CostShare=服务器文本='<%#的eval(CostShare,{0:$ 0.00})%&GT ;'的CausesValidation =真/>
            < ASP:CompareValidator ID =vld_CostShare
                                    =服务器
                                    的ControlToValidate =tb_CostShare
                                    操作=DataTypeCheck
                                    TYPE =货币
                                    的ValidationGroup =VLD
                                    的ErrorMessage =你必须输入金额为成本分担。 />


解决方案

该CompareValidator并不支持货币符号。您可以preFIX您的输入控制与$或使用常规的前pression验证,页面有一个例子。

下面的模式将匹配你的例子( http://www.regexlib.com 提供):

  ^ \\ $([0-9] {1,3},([0-9] {3},)* [0-9] {3} |?[ 0-9] +)。(。[0-9] [0-9])?$

I have created the TextBox and CompareValidator below which I thought would allow input in the following forms:

  • 5
  • 5.00
  • $5.00

Unfortunately it's not allowing the version with the dollar sign in it. What is the point of doing a type check against currency if you don't allow the dollar sign? Is there a way to allow this symbol?

            <asp:TextBox ID="tb_CostShare" runat="server" Text='<%# Eval("CostShare", "{0:$0.00}")%>' CausesValidation="true" />
            <asp:CompareValidator   ID="vld_CostShare" 
                                    runat="server" 
                                    ControlToValidate="tb_CostShare" 
                                    Operator="DataTypeCheck" 
                                    Type="Currency" 
                                    ValidationGroup="vld" 
                                    ErrorMessage="You must enter a dollar amount for 'Cost Share'." />    

解决方案

The CompareValidator doesn't support currency symbols. You can prefix your input control with the $ or use a regular expression validator, this page has an example.

The following pattern will match your examples (courtesy of http://www.regexlib.com):

^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$

这篇关于验证货币输入的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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