Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date validation and formatting using Javascript

Posted on 2014-10-23
3
Medium Priority
?
149 Views
Last Modified: 2014-10-29
Hi Experts,

I want to validate and format date field (mm/dd/yy) using javascript and am looking for sample code snippet.
Valid year is in between 1938 to 2050.

Valid i/p         o/p (Validation & Formatting)

12345-->         01/23/45
1232009-->     01/23/09
10122014-->   10/12/14
101214--->      10/12/14
10/20/2014--> 10/20/14
10/20/14-->     10/20/14
10-20-14-->     10/20/14
10-20-2014--> 10/20/14

Invalid Dates

12/89/2014->Invalid Date format,since we can't have date 89
13/12/2014->Invalid Date format,since we can't have month 13
1234567890->Invalid date format

Thanks in Advance
0
Comment
Question by:ksd123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 40400963
you should(must) go this logic and use a datepicker : http://jqueryui.com/datepicker/
or at least use this : http://digitalbush.com/projects/masked-input-plugin/
HTML5 : http://www.w3.org/TR/html-markup/input.date.html
0
 
LVL 58

Expert Comment

by:Gary
ID: 40400982
It's crazy to allow any kind of input like that and try and work out a date from it - follow leakim's advice

What about:
11111
Is that supposed to 1/11/11 or 11/1/11
0
 
LVL 34

Expert Comment

by:Slick812
ID: 40401013
????
You really must use a more restricted set of "inputs" as your first example CAN NOT meet your specifications -
12345-->   01/23/45

you specify this - "year is in between 1938 to 2050"
so this -
12345  could be either 1945  OR  2045, a difference of 100 years, so your inputs can not give correct outputs

as to invalid date check, this is not usually dificult with dates in this format -
    3/19/2014

BUT that format is NOT the same as you give in the first examples as -
    10/12/14

I ask you "Where does the text date input come from? ?"
a server database SELECT, user input, a web REST service?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

715 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