Solved

Date validation and formatting using Javascript

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell- Registry HIVE 6 63
How to create a registry Subkey containing the hostname 7 38
Drag & Drop Error 5 32
Javascript function 3 21
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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)

751 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