how to Check whether entered value in textbox is string or integer in asp.net c#?


Sometimes we need to know entered value is string or integer we can use int.TryParse method…
How to use int.TryParse method:
it will take one input and output variables and it returns bool(if int returns true else false)

Code in .aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="IntorNot.aspx.cs" Inherits="IntorNot" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title>Int or Not</title>
   
    <style type="text/css">
        .style1
        {
            font-weight: bold;
        }
    </style>
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table>
            <tr>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Integer or not"
                        onclick="Button1_Click" />
                    </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" CssClass="style1" style="color: #009900"></asp:Label>
                    <asp:Label ID="Label2" runat="server" CssClass="style1" style="color: #CC0000"></asp:Label>
                    </td>
                <td>
                    
                    </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

Code in .aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class IntorNot : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(TextBox1.Text))
        {
            int EnteredIntValue = 0;
            bool IsIntOrNot = false;
            IsIntOrNot = int.TryParse(TextBox1.Text, out EnteredIntValue);
            if (IsIntOrNot)
            {
                Label1.Text = "Yes, You Entered " + EnteredIntValue.ToString() + " is an Integer";
                Label2.Text = string.Empty;
            }
            else
            {
                Label2.Text = "No, You Entered " + TextBox1.Text + " is Not an Integer";
                Label1.Text = string.Empty;
            }
        }
        else
        {
            Label2.Text = "Please Enter Something in textbox";
            Label1.Text = string.Empty;
        }
    }
}

Output:

it's an integer

 Not an integer

 Please enter something in textbox

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s