How to Validate Integer 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 integer 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 Integer using Asp.Net. Basically TryParse method will help us to check user integer inputs. It Converts the specified string representation of an Integer to its equivalent Integer and returns a value that indicates whether the conversion succeeded or not. In the below example I will show you how to validate Integer 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 Integer Number Value from Server Side using Asp.Net Csharp

To do the Example First Create a Simple UI:

    <asp:Label ID="lbl_integer" runat="server" Text="Enter Integer: "></asp:Label>
    <asp:TextBox runat="server" ID="txt_integer"></asp:TextBox>
    <asp:Button ID="cmd_integer" runat="server" Text="Click to check" OnClick="cmd_integer_Click" /><br />
    <asp:Label ID="lbl_integer_result" runat="server" Font-Bold="True"></asp:Label>

Now Validate the Integer Number under Button Click event:
C# Code:

    protected void cmd_integer_Click(object sender, EventArgs e)
        Int32 Check_Integer;
        if (Int32.TryParse(txt_integer.Text, out Check_Integer))
            lbl_integer_result.Text = "Integer Validation Successfull.";
            lbl_integer_result.Text = "Invalid Integer!!";

VB.Net Code:

    Protected Sub cmd_integer_Click(sender As Object, e As EventArgs)
        Dim Check_Integer As Integer
        If Int32.TryParse(txt_integer.Text, Check_Integer) Then
            lbl_integer_result.Text = "Integer Validation Successfull."
            lbl_integer_result.Text = "Invalid Integer!!"
        End If
    End Sub

Hope now you can validate any integer 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 *