Nick Kapatais
Nick Kapatais

Reputation: 336

ASP TextBox support for decimal numbers with a . (dot)

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?

Upvotes: 1

Views: 12539

Answers (2)

Nitin Kinger
Nitin Kinger

Reputation: 31

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>

Upvotes: 1

ashish shiwalkar
ashish shiwalkar

Reputation: 121

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>

Upvotes: 2

Related Questions