Link to home
Start Free TrialLog in
Avatar of ksd123
ksd123

asked on

Date validation and formatting using Javascript

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
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
????
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?