Solved

convert 2 digit year to 4 digit

Posted on 2006-06-26
4
5,116 Views
Last Modified: 2008-01-09
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.

Thanks in advance for your help.
Clint...
0
Comment
Question by:clintnash
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:netsmithcentral
ID: 16987894
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
 
LVL 1

Author Comment

by:clintnash
ID: 16988359
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?

thanks for your help,
Clint...
0
 
LVL 12

Accepted Solution

by:
netsmithcentral earned 500 total points
ID: 16988424
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
 
LVL 1

Author Comment

by:clintnash
ID: 16988531
That fixed it perfectly.  Its converting to 4 digits and now validating the input.

Thanks so very much for your time today,
Clint...
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now