Solved

Date validation and formatting using Javascript

Posted on 2014-10-23
3
146 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 500 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

622 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