View Single Post
Old 11-01-2012, 09:27 PM   PM User | #1
hardhitter06
New Coder

 
Join Date: Jun 2009
Posts: 90
Thanks: 14
Thanked 0 Times in 0 Posts
hardhitter06 is an unknown quantity at this point
Validate Date field MM/DD/YYYY with legit date

Hi All,

I've been searching all over the web and plugging in pieces of code that I have found that would accomplish a field validation maknig sure the date is in the right format and it is an actual date. Not 12/45/2012.

This is the latest piece of code I've taken from the web but this still isn't working:

Code:
function isValidDate(dateString)
{
    // First check for the pattern
    if(!/^\d{2}\/\d{2}\/\d{4}$/.test(dateString))
        return false;

    // Parse the date parts to integers
    var parts = dateString.split("/");
    var day = parseInt(parts[1], 10);
    var month = parseInt(parts[0], 10);
    var year = parseInt(parts[2], 10);

    // Check the ranges of month and year
    if(year < 1000 || year > 3000 || month == 0 || month > 12)
        return false;

    var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

    // Adjust for leap years
    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
        monthLength[1] = 29;

    // Check the range of the day
    return day > 0 && day <= monthLength[month - 1];
}
And I also have:

Code:
function validateForm( frm )
{
	var oops = "";
	var apptype = getRBValue( frm.elements["MoveType."] );
    if ( apptype == null )
        oops += "You must check Temporary or Permanent.\n";
	var apptype = getRBValue( frm.elements["ResSummer."] );
    if ( apptype == null )
        oops += "You must check Yes or No for residential summer student.\n";
    if ( trim( frm.elements["FirstName."] ).length  < 2 )
        oops += "You must enter your First name.\n";
    if ( trim( frm.elements["LastName."] ).length  < 2 )
        oops += "You must enter your Last name.\n";
    if ( ! emailCheck( frm.elements["Email."] ) )
        oops += "That does not appear to be a valid Email Address.\n";
	if (trim( frm.elements["Box."] ).length  < 1 || trim( frm.elements["Box."] ).length > 4)
        oops += "You must enter a Box# (Max 4 digits).\n";
    if ( ! isValidDate( frm.elements["EffectiveDate."] ) )
        oops += "You must enter your Effective Date of Move in the proper format (MM/DD/YYYY).\n";	
if ( trim( frm.elements["StreetAddress."] ).length  < 5 )
        oops += "You must enter your Street Address.\n";
	if ( trim( frm.elements["City."] ).length  < 4 )
        oops += "You must enter your City.\n";
    if ( trim( frm.elements["State."] ).length  < 1 )
        oops += "You must enter your State.\n";
	if ( ! zipCheck( frm.elements["Zip."] ) )
        oops += "You must enter a 5 digit Zip Code.\n";
    // additional building and room are not validated, but they are are trimmed and upper cased
    trim(frm.Apt);
	
    
    var inps = frm.elements;
    var chkbx = false;
    for ( var i = 0; i < inps.length; ++i )
    {
        var inp = inps[i];
        if ( inp.name.indexOf("ChkBx") > 0 && inp.value != "" && inp.checked )
        {
            var chkbx = true;
            break;
        }
    }


    if ( oops == "" ) return true;
	alert("ERROR(S):\n" + oops);    
    return false;
}
Can someone show me what I'm going wrong or if there's a better piece of code out there? When I leave the field blank I get the error code but when I enter a legit date with the proper format I still get the error...
hardhitter06 is offline   Reply With Quote