How to Validate Decimal Double Float DataType Number Value from Server Side using Asp.Net C# VB.Net

In most cases Asp.Net C# VB.Net developers need to check or validate user input for Decimal, Double or Float value or number. C# provides us an easy way to validate those user inputs from server side. Here in this small article I will show you how one can validate Decimal, Double or Float using Asp.Net. Basically TryParse method will help us to check user Decimal, Double or Float inputs. It Converts the specified string representation of a Decimal, Double or Float to its equivalent Decimal, Double or Float and returns a value that indicates whether the conversion succeeded or not. In the below example I will show you how to validate Decimal, Double or Float values aside from client end validation.

Related Article:
Cross-browser javascript onkeypress IsNumeric() method for Number Validation Asp.Net C# VB.Net

How to Validate Decimal Double Float DataType Value from Server Side using Asp.Net Csharp

To do the Example First Create a Simple UI:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label ID="lbl_decimal" runat="server" Text="Enter Decimal: "></asp:Label>
    <asp:TextBox runat="server" ID="txt_decimal"></asp:TextBox>
    <asp:Button ID="cmd_decimal" runat="server" Text="Click to check" OnClick="cmd_decimal_Click" /><br />
    <asp:Label ID="lbl_decimal_result" runat="server" Font-Bold="True"></asp:Label>

Now Validate the Decimal, Double or Float Number under Button Click event:
C# Code:

    protected void cmd_decimal_Click(object sender, EventArgs e)
        decimal check_decimal;
        if (decimal.TryParse(txt_decimal.Text, out check_decimal))
            lbl_decimal_result.Text = "Decimal Validation Successfull.";
            lbl_decimal_result.Text = "Invalid Decimal!!";

VB.Net Code:

    Protected Sub cmd_decimal_Click(sender As Object, e As EventArgs)
        Dim check_decimal As Decimal
        If Decimal.TryParse(txt_decimal.Text, check_decimal) Then
            lbl_decimal_result.Text = "Decimal Validation Successfull."
            lbl_decimal_result.Text = "Invalid Decimal!!"
        End If
    End Sub

Hope now you can validate any Decimal, Double or Float number value using from server side. Server side validation is also a good practice along with client side validation.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *