C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...







Custom Validation for Date Field


Posted Date: 01 Apr 2006    Resource Type: Articles    Category: .NET Framework

Posted By: Chaitanya Prasad       Member Level: Bronze
Rating:     Points: 10



=======================================================

Steps to be fllowed to write a custom Validator:


------------------------------------------------------
Scenario:
Date Of Birth Validation:

//If the user enters date greater than the current
//date of the server then display an error message

1)Create a hidden field control in aspx page in order to have server date rather than client machine date

<asp:HiddenField ID="hdnCurrentDate"  runat="server" />


2)Convert the date to required format from code behind aspx.cs file
//Setting the current date value to hidden field
//This Current date is sent to a function to check the
//Date Of Birth

hdnCurrentDate.Value = DateTime.Now.ToString("dd-MM-yyyy");


3)Add a Custom validator for the required date field
<asp:CustomValidator ID="custDateOfBirth"  runat="server" 	ControlToValidate="txtBirthDate" ClientValidationFunction="CheckDate" ErrorMessage="Enter less than the current date" CssClass="ClsRedtext"  SetFocusOnError="true"></asp:CustomValidator>

4)Write a javascript function to handle the validation

<script>
function CheckDate(oSrc,args)
{
//The below two ids are view source of the aspx file
//of the respective controls one is Entered date and the other is hidden field date
//('ctl00_ContentPlaceHolder1_txtBirthDate')
//('ctl00_ContentPlaceHolder1_hdnCurrentDate')

var dateOfBirthClientID = document.getElementById('ctl00_ContentPlaceHolder1_txtBirthDate');
var systemDateClientID = document.getElementById('ctl00_ContentPlaceHolder1_hdnCurrentDate');

var dateOfBirth = dateOfBirthClientID.value;
var systemDate = systemDateClientID.value;

var ArrDob = dateOfBirth.split("-");

var DobMonth = ArrDob[0];
var DobDate = ArrDob[1];
var DobYear = ArrDob[2];

var ArrSysDate = systemDate.split("-");

var SysDate = ArrSysDate[0];
var SysMonth = ArrSysDate[1];
var SysYear = ArrSysDate[2];

//alert("DobYear="+DobYear+"SysYear ="+SysYear +"DobMonth="+DobMonth+"SysMonth="+SysMonth+"DobDate="+DobDate+"SysDate="+SysDate);

if(DobYear > SysYear)
{
args.IsValid = false;
return;
}
else if((DobYear == SysYear) && (DobMonth > SysMonth))
{
args.IsValid = false;
return;
}

else if((DobYear == SysYear) && (DobMonth == SysMonth) && (DobDate > SysDate))
{
args.IsValid = false;
return;
}
args.IsValid = true;
}
</script>

=======================================================

Its a good example to start with a small custom control for starters







Responses

Author: Satyanarayan SushilKumar Bajoria    20 May 2008Member Level: Gold   Points : 2
nice stuff and give some more


Author: vasanthiraajan    30 May 2008Member Level: Gold   Points : 2
hi,

Very nice.

Thanks.


Author: Ashutosh Kumar    31 May 2008Member Level: Gold   Points : 2
Nice article

Thanks,


Author: Mahesh Raj    07 Jun 2008Member Level: Gold   Points : 1
This is very good information,Continue posting such useful articles.


Author: Raghuramakarthikeyan    08 Aug 2008Member Level: Gold   Points : 1
why i was disapproved in Google Adsense.I didnt Click My ads.then What Is The Problem.Can You Tell ME.


Author: purushothaman    09 Aug 2008Member Level: Silver   Points : 1
hi Prasad ,

Thanks,
i struggle a lot before ,
now my problem solve
nice Information Handing dates in javascripts


Author: Geetha    13 Aug 2008Member Level: Gold   Points : 0
HI very Nice.
Thanks a lot.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Create virtual Directory in IIS programmatically
Previous Resource: Creating a Singleton Object
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design   Scripts

conference calls

Contact Us    Privacy Policy    Terms Of Use