# convert 2 digit year to 4 digit

Hello.

In the interest of making my application friendly to whiny users, I need to allow a date to be entered as mm/dd/yy AND mm/dd/yyyy.  I have modified my mask to allow this and everything works except my date validation script which REQUIRES that the date be in mm/dd/yyyy format.

I would like help in creating a function that will ONBLUR check to see if the date is mm/dd/yy and if so add the appropriate 19 or 20 in front of the 2 digit year so that my validation will work properly.  If its already mm/dd/yyyy then do nothing.

Clint...
LVL 1
###### Who is Participating?

Commented:
Whoops, sorry.  I rejoined the array wrong.  This should fix it:

function yrChk(){
var date = this.value;
var yr = date.split('/')[2];
if(yr.length==2) yr = (yr>50) ? '19'+yr : '20'+yr;
date = date.split('/'); date[2] = yr; date = date.join('/');
this.value = date;
}
0

Commented:
No problem.  Here's the function you would want to register to the onblur event:

function yrChk(){
var date = this.value;
var yr = date.split('/')[2];
if(yr.length==2) yr = (yr>50) ? '19'+yr : '20'+yr;
date = date.split('/'); date[2] = yr; date.join('/');
this.value = date;
}

0

Author Commented:
Great, with one small problem.  I attached your script and pass it the control which does change the two digit date to a four digit date, however it produces a strange result.

I enter in 11/11/80 and tab off, it changes the value to

11,11,1980

Any idea on how best to change those commas back to slashes?

Clint...
0

Author Commented:
That fixed it perfectly.  Its converting to 4 digits and now validating the input.

Thanks so very much for your time today,
Clint...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.