Best way to check currency entry?

I created a TextBox and CompareValidator, below which, as I thought, allowed input in the following forms:

  • 5
  • 5.00
  • $ 5.00

Unfortunately, this does not allow the dollar sign version in it. What is the point of doing a type check against a currency if you have not allowed a dollar sign? Is there any way to enable this character?

<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'." /> 
+6
c # validation
source share
2 answers

CompareValidator does not support currency symbols. You can prefix input controls with $ or use the regex validator, this page has an example.

The following sample 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])?$ 
+8
source share

In addition, you can write a special validator for parsing a string with or without $. But you will need to write Javascript to get client side confirmation.

+1
source share

All Articles