Sunday, February 7, 2010

How to validate Integer Decimal Double DateTime values in Asp.Net C#

Please visit my new Web Site

In most cases C# VB.Net developers need to check or validate user input for integer, decimal & DateTime. 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 using Asp.Net. Basically TryParse boolean returntype method will help us to check user inputs. It Converts the specified string representation of a Date and time / Integer / Decimal to its DateTime equivalent Integer equivalent Decimal equivalent and returns a value that indicates whether the conversion succeeded or not. In the below example i will show you to validate DateTime, Integer & Decimal values validation.

The output like:
How to validate integer,datetime & Decimal datatatype using

To do that add a page in your project & paste the below HTML MARKUP:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="validate_int_datetime.aspx.cs" Inherits="validate_int_datetime" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
    <title>How to validate integer double numeric datetime in</title>
    <form id="form1" runat="server">
    <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" />
    <asp:Label ID="lbl_integer_result" runat="server" Font-Bold="True"></asp:Label>
    <br />
    <hr />
    <br />
    <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" />
    <asp:Label ID="lbl_decimal_result" runat="server" Font-Bold="True"></asp:Label>
    <br />
    <hr />
    <br />
    <asp:Label ID="lbl_Datetime" runat="server" Text="Enter datetime: "></asp:Label>
    <asp:TextBox runat="server" ID="txt_Datetime"></asp:TextBox>
    <asp:Button ID="cmd_Datetime" runat="server" Text="Click to check" OnClick="cmd_Datetime_Click" />
    <asp:Label ID="lbl_datetime_result" runat="server" Font-Bold="True"></asp:Label>
Now in codebehind write the below code:
using System;

public partial class validate_int_datetime : System.Web.UI.Page
    Int32 Check_Integer;
    decimal check_decimal;
    DateTime check_Datetime;
    protected void Page_Load(object sender, EventArgs e)

    protected void cmd_integer_Click(object sender, EventArgs e)
        if(Int32.TryParse(txt_integer.Text,out Check_Integer))
            lbl_integer_result.Text="Integer Validation Successfull.";
            lbl_integer_result.Text = "Invalid Integer!!";
    protected void cmd_decimal_Click(object sender, EventArgs e)
        if (decimal.TryParse(txt_decimal.Text, out check_decimal))
            lbl_decimal_result.Text = "Decimal Validation Successfull.";
            lbl_decimal_result.Text = "Invalid Decimal!!";
    protected void cmd_Datetime_Click(object sender, EventArgs e)
        if (DateTime.TryParse(txt_Datetime.Text, out check_Datetime))
            lbl_datetime_result.Text = "Datetime Validation Successfull.";
            lbl_datetime_result.Text = "Invalid Datetime!!";
Check the code how i use TryParse to validate datatypes.

Hope now you can validate any primitive datattypes using


Want to say something?

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash