Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:03:48+00:00 2026-06-01T14:03:48+00:00

I’ve inherited the following script and it needs to be improved upon some. The

  • 0

I’ve inherited the following script and it needs to be improved upon some. The function checks that only allowed characters are entered (0123456789/) and then formats a date entered as 1/1/12 will be reformatted as 01/01/2012. This part works just fine after a little tweaking. I now need to take the validation a step further and add the year if it is omitted meaning if a user enters 1/1, it needs to be formatted and have the current year added (e.g. 01/01/2012).

Example of user inputs and required (working) outputs

  • a/a/a alert user of error – check
  • 1/2/10 updates input field to read as 01/03/2010
  • 01/01/12 updates input field to read as 01/01/2012
  • 1/10/2 updates input field to read as 01/10/2002

Desired Update (in addition to above)
1/9 updates input field to read as 01/09/2012

Here is the current function (you are welcome to change, rewrite, whatever, as long as the above functionality is retained). jQuery 1.7 library is in use and can be implemented.

function ValidateDate(obj)
{
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid dates with 2 digit month, 2 digit day, 
    4 digit year. Date separator has to be /
    Uses combination of regular expressions and 
    string parsing to validate date.
    Ex. mm/dd/yyyy

PARAMETERS:
   ValidateDate(strValue) - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
    var checkOK = "0123456789/";
    var checkStr = obj.value;
    var allValid = true;
    var p = /(\d{1,2})\/(\d{1,2})\/(\d{1,4})/;
    var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
    // check to see if valid characters were used       
    for (i = 0;  i < checkStr.length;  i++)
    {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
          if (ch == checkOK.charAt(j))
            break;
        if (j == checkOK.length)
        {
            allValid = false;
            break;
        }
    }
    if (!allValid)
    {
        alert("Please use only a combination of " + checkOK + "\'s charaters in the date field.  Dates should be entered in the format of mm/dd/yyyy.");
        setTimeout((function() { obj.select() }), 0);
        return (false);
    }       
    //  converts to mm/dd/yyyy format
    if (!obj.value.match(p)) return;
    num=new Array();
    num=obj.value.match(p);
    if (num[1].length == 1) num[1]="0" + num[1];
    if (num[2].length == 1) num[2]="0" + num[2];
    if (num[3].length == 1) num[3]="200" + num[3];
    if (num[3].length == 2) num[3]="20" + num[3];
    obj.value= num[1] + "/" + num[2] + "/" + num[3];
    //check to see if in correct format
    if(!objRegExp.test(obj.value))
    {
        alert('The date entered is not properly formatted.');
        return false; //doesn't match pattern, bad date
    }
    else{
        var arrayDate = obj.value.split(RegExp.$1); //split date into month, day, year
        var intDay = parseInt(arrayDate[1],10); 
        var intYear = parseInt(arrayDate[2],10);
        var intMonth = parseInt(arrayDate[0],10);
    //check for valid month
    if(intMonth > 12 || intMonth < 1) {
        alert('The date entered is invalid');
        return false;
    }

    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }

    //check for February
    var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
        return true; //Feb. had valid number of days
    }
    alert(obj.value + ' is not a valid date.');
    //  return false; //any other values, bad date
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:03:49+00:00Added an answer on June 1, 2026 at 2:03 pm

    Some points to object:

    • You don’t have to check for valid characters, because your match regexp already needs them. If you really want to, use /^[\d\/]*$/.test() instead of that loop.
    • To match dates like 1/1, use /\d{1,2}\/\d{1,2}(\/\d{1,4})?/ as p and just do a num = obj.value.split("/") instead of matching groups
    • To validate the date, have a look at javascript date validation using date object
    • You should also allow ISO date format YYYY-MM-DD, which is parsed natively by Date()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.