Solved

Date validation and formatting using Javascript

Posted on 2014-10-23
3
113 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
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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
Comment Utility
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 33

Expert Comment

by:Slick812
Comment Utility
????
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

16 Experts available now in Live!

Get 1:1 Help Now