1

I have an ASP page and in that page resides a TextBox. This TextBox has a Type of Double and in the back end code has a Decimal type.

Here is the TextBox.

            <asp:TableCell>
                <asp:TextBox ID="txtPremium" runat="server" Width="90px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvPremium" runat="server" ControlToValidate="txtPremium"
                    ErrorMessage="Required" ForeColor="Red" Display="Dynamic" ValidationGroup="Insert"></asp:RequiredFieldValidator>
                <asp:CompareValidator ID="txtPremium_Integer" runat="server" ValidationGroup="Insert"
                    ControlToValidate="txtPremium" Display="Dynamic" ErrorMessage="'Premium' must be decimal"
                    ForeColor="Red" Operator="DataTypeCheck" SetFocusOnError="true" Type="Double" />
            </asp:TableCell>

In the UI, when I try to add a value into that text box such as 10.0 (with a dot) the text box error message appears. When I try to enter a value like 10,0 (with a comma) , it's accepted. I need to be able to enter a dot.

I've tried changing the Type of the text box to Currency but the same error occurs. No other relevant types are available in the Type list of the text box.

For the sake of completion here is the declaration of the text box in my code behind.

Dim _premium As Decimal = Decimal.Parse(txtPremium.Text)

I'm not sure as to what I can do for the text box to accept dots.

Any ideas?

Nikos
  • 326
  • 2
  • 4
  • 18
  • Does the language/region settings for your PC/the ASP host use a European number format that would treat `,` as the separator between digits and decimals instead of `.`? – A Friend Jul 12 '18 at 10:00
  • Oh I don't know I'm using a customers computer. How can I check this? – Nikos Jul 12 '18 at 10:01
  • Possible duplicate: https://stackoverflow.com/questions/19893407/string-to-decimal-conversion-dot-separation-instead-of-comma/19893597 – Valentin Sky Jul 12 '18 at 10:03
  • 1
    You can create your own `CustomValidator` to check for decimal numbers if both comma and dot are correct inputs: `€ 1,95` and `$ 1.95`. @ValentinSky, it's not a duplicate since OP is asking about Validation Controls. – VDWWD Jul 12 '18 at 10:18
  • I have updated the code. – Nikos Jul 12 '18 at 10:22
  • Previous code unfortunately did not work. I removed it to avoid confusion. I'm now trying VDWWD's option and I'll get back. – Nikos Jul 12 '18 at 10:33
  • Do you mean using a regular expression VDWWD? – Nikos Jul 12 '18 at 10:40
  • Tried with regex but didn't work. VDWWD I looked at CustomValidators but I can't seem to find information on how to use it. I'm finding String examples. – Nikos Jul 12 '18 at 10:45
  • Set `Type="Currency" ` and try – Developer Jul 12 '18 at 11:22
  • Already tried that. – Nikos Jul 12 '18 at 11:23
  • What was the issue with that? it will accept 2 decimal values after dot – Developer Jul 12 '18 at 11:30
  • You can check here https://stackoverflow.com/questions/3613939/possible-to-get-the-comparevalidator-to-accept-numbers-with-commas – Developer Jul 12 '18 at 11:30
  • @DorababuMeka As I’ve stated in the question, I have already tried “Currency”. When I put “Currency” the text boxes error message appears just like it does with “Double”. – Nikos Jul 12 '18 at 11:37

2 Answers2

2

Hi compare validator is used to compare values between two fields, eg password and matching password

if you want to validate for decimal use regular expression validator with expression accepting decimal instead. replace 2 in following expression with max allowed decimal valu ^\d+.\d{0,2}

    <asp:RegularExpressionValidator runat="server" ErrorMessage="Decimal Only" ID="txtregpre" ValidationGroup="Insert"
                       ControlToValidate="txtPremium"              
ValidationExpression="^\d+\.\d{0,2}$"></asp:RegularExpressionValidator>
  • if you want to allow coma expression will change – ashish shiwalkar Jul 12 '18 at 11:26
  • use this for comma ^[1-9][\.\d]*(,\d+)?$ – ashish shiwalkar Jul 12 '18 at 11:28
  • I’ll try that tomorrow and come back. Thanks. – Nikos Jul 12 '18 at 11:28
  • It works. Just a few clarifications. Could you explain exactly what this means? "^\d+\.\d{0,2}$" – Nikos Jul 13 '18 at 04:53
  • its a regular expression d for decimal d{0,2} means decimal unto 2 digit eg 1.12 for further reading check https://en.wikipedia.org/wiki/Regular_expression and for in depth regular excretion in vb check https://www.tutorialspoint.com/vb.net/vb.net_regular_expressions.htm (probabaly overkill) just remember your text will be validated according to regular expression if its valid no error is raised you can validate all patterns in string like email, decimal string with particular comma and decimal point ,date in particular format etc – ashish shiwalkar Jul 13 '18 at 05:07
1

You can use regular expression instead of compare validation, here is example working for both 10.0 and 10,0

   <asp:TableCell>
        <asp:TextBox ID="txtPremium" runat="server" Width="90px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvPremium" runat="server" ControlToValidate="txtPremium"
            ErrorMessage="Required" ForeColor="Red" Display="Dynamic" ValidationGroup="Insert"></asp:RequiredFieldValidator>
        <!-- Your Regular Expression Validator -->
        <asp:RegularExpressionValidator ID="txtPremium_Integer"
            ControlToValidate="txtPremium" ValidationGroup="Insert" ForeColor="Red"
            runat="server" Display="Dynamic"
            ErrorMessage="'Premium' must be decimal"
            ValidationExpression="^\d+([,\.]\d{1,2})?$">
        </asp:RegularExpressionValidator>
    </asp:TableCell>