Wrox Programmer Forums
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 29th, 2004, 04:45 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Date validation

Hi all
I want to validate date in all respect like leap year and date of every month in C#. My format of date is dd/mm/yyyy.

Can anyone help me at this regard.
I have tried with regular expression also but that also is not helpful.

 
Old September 29th, 2004, 06:36 AM
Authorized User
 
Join Date: Jul 2004
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aravwind Send a message via Yahoo to aravwind
Default

Wat kind of help r u expecting...
Do u need the entire code for the Date function?
 
Old September 29th, 2004, 07:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Try this:

http://www.eworldui.net/CustomContro...PopupDemo.aspx

Very handy.

 
Old September 29th, 2004, 08:16 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes i want the entire code or function to validate the date.
I want the user to allowed only valid date i.e he must not be allowed to enter 29th of Feb except leap year or 31 st of any month if the particular month is having only 30 days.
Please reply as soon as possible.

 
Old September 30th, 2004, 02:56 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

Lily Here is the code in javascript to validate date.Convert to C# and do..

<script language = "Javascript">
/**
 * DHTML date validation script. Courtesy of SmartWebby.com
(http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
         var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
         var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
         // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 ==
0))) ? 29 : 28 );
}
function DaysArray(n) {
         for (var i = 1; i <= n; i++) {
                  this[i] = 31
                  if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
                  if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
         var daysInMonth = DaysArray(12)
         var pos1=dtStr.indexOf(dtCh)
         var pos2=dtStr.indexOf(dtCh,pos1+1)
         var strMonth=dtStr.substring(0,pos1)
         var strDay=dtStr.substring(pos1+1,pos2)
         var strYear=dtStr.substring(pos2+1)
         strYr=strYear
         if (strDay.charAt(0)=="0" && strDay.length>1)
strDay=strDay.substring(1)
         if (strMonth.charAt(0)=="0" && strMonth.length>1)
strMonth=strMonth.substring(1)
         for (var i = 1; i <= 3; i++) {
                  if (strYr.charAt(0)=="0" && strYr.length>1)
strYr=strYr.substring(1)
         }
         month=parseInt(strMonth)
         day=parseInt(strDay)
         year=parseInt(strYr)
         if (pos1==-1 || pos2==-1){
                  alert("The date format should be : mm/dd/yyyy")
                  return false
         }
         if (strMonth.length<1 || month<1 || month>12){
                  alert("Please enter a valid month")
                  return false
         }
         if (strDay.length<1 || day<1 || day>31 || (month==2 &&
day>daysInFebruary(year)) || day > daysInMonth[month]){
                  alert("Please enter a valid day")
                  return false
         }
         if (strYear.length != 4 || year==0 || year<minYear ||
year>maxYear){
                  alert("Please enter a valid 4 digit year between
"+minYear+" and "+maxYear)
                  return false
         }
         if (dtStr.indexOf(dtCh,pos2+1)!=-1 ||
isInteger(stripCharsInBag(dtStr, dtCh))==false){
                  alert("Please enter a valid date")
                  return false
         }
return true
}

function ValidateForm(){
         var dt=document.frmSample.txtDate
         if (isDate(dt.value)==false){
                  dt.focus()
                  return false
         }
    return true
 }

</script>










Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Validation aftabn10 PHP How-To 0 January 29th, 2007 10:35 AM
Date Validation ramesh055 ASP.NET 1.0 and 1.1 Professional 1 November 15th, 2006 11:09 AM
date validation (again) crmpicco Javascript How-To 14 March 29th, 2006 06:58 PM
Date Validation surendran Javascript How-To 3 February 25th, 2005 07:40 AM
Date validation Raul Javascript 4 February 25th, 2004 04:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.