Friday 3 October 2014

Regex to validate date format dd/mm/yyyy in asp.net using C#


Regex Validation for Datetime

Description:-




In this example we explain that how to validate Date format in dd/mm/yyyy in asp.net C# backend code at runtime validation using Regex validator provided by .Net Framework.

We all know very to validate any like Date,only number,required validation,Range validation is easily at client side but my problem is that it will be validated by runtime.

For example in my real project in which one form name like create report in which form one is dropdown and its contain three items like string,integer and Date. And second control is textbox and one submit button. Now condition is that if user select string item from dropdown list then only string are allow to enter in below Textbox control same like if user select Date item from dropdownlist then only user allow to enter Date only and it also in dd/mm/yy format and same for Integer items. So it is difficult task to validate at runtime for me at first time.and then finally I got the solution are described bellows.

Dynamically Bind Data in Ajax Accordion Panel Bind Data to Accordion from databse


Restrict the size of File when Uploaded How to Restrict the size of File when uploaded by user


 Code:-

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

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


        TextBox txtDisplayName = GridWrap.FindControl("txtDisplayName") as TextBox;
        DropDownList drpDatatype = GridWrap.FindControl("drpDatatype") as DropDownList;
        DropDownList drpRequiresInput = GridWrap.FindControl("drpRequiresInput") as DropDownList;
        TextBox txtMaxLength = GridWrap.FindControl("txtMaxLength") as TextBox;
        DropDownList drpDefaultValueType = GridWrap.FindControl("drpDefaultValueType") as DropDownList;
        TextBox txtDefaultValue = GridWrap.FindControl("txtDefaultValue") as TextBox;
        //commented by kirit
        if (drpDatatype.SelectedValue.ToString().Equals("DateTime"))  //here check if datatype is datetime then check in date and time for textbox value
        {
            if (!drpDefaultValueType.Text.Equals("Null"))
            {
                var match = Regex.Match(txtDefaultValue.Text, @"^(((0?[1-9]|1[012])-(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])-(29|30)|(0?[13578]|1[02])-31)-(19|[2-9]\d)\d{2}|0?2/29-((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$");
                if (!match.Success)
                {
                    trMessage.Show(lblMessage.ReportError("Please enter Date mm-dd-yyyy Properly."));
                    return false;

                }
            }
        }
        else if (drpDatatype.SelectedValue.ToString().Equals("Integer")) //here check if datatype is Integer then check in Integer for textbox value
        {
            if (!drpDefaultValueType.Text.Equals("Query"))
            {
                var match = Regex.Match(txtDefaultValue.Text, @"^[0-9]*$");
                if (!match.Success)
                {
                    trMessage.Show(lblMessage.ReportError("Please enter only Number value in Textbox."));
                    return false;
                }
            }
        }
        else if (drpDatatype.SelectedValue.ToString().Equals("String")) //here check if datatype is String then check in String for textbox value
        {
            if (drpDefaultValueType.Text.Equals("SpecifiedValue"))
            {
                if (txtDefaultValue.Text.Trim().Equals(""))
                {
                    trMessage.Show(lblMessage.ReportError("Please enter value in Textbox."));
                    return false;

                }
            }
        }
    }
}

0 comments:

Post a Comment