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
Solved

Date Validation - Can't enter date in the past

Posted on 2007-12-05
2
969 Views
Last Modified: 2011-09-20
Hi, I'm trying to validate my date field so that if a user puts a date in the past, an alert pops up. I'm currently using this script:

var now = new Date().getTime();
var generatedDate = Date.parse(form1.seminarsDate.value); // assuming a valid date format mm/dd/yyyy
if(document.form1.seminarsDate.value==""){
      alert("Please enter a Date");
      document.form1.seminarsDate.focus();
return false;
}
else if (now>generatedDate) {
      alert('Please enter a date that is not in the past!');
      form1.seminarsDate.focus();
return false;
}
return true

This works great, but the only problem is I need it for the UK, i.e. using DD/MM/YYYY and not MM/DD/YYYY as this script uses.

Any ideas?

Many thanks

Chris
0
Comment
Question by:djfenom
2 Comments
 
LVL 2

Accepted Solution

by:
duckets earned 500 total points
ID: 20410212

Try replacing your second line of code with the following lines attached below. They break apart the string (using the slash symbol as the delimiter), and create a new date object based on the assumed DD/MM/YYYY order of the elements. Note the 'month' has a -1 appended to it, because when creating a date object, months are specified in the range 0-11!


// assumes an input in the UK format: DD/MM/YYY
dateElements = form1.seminarsDate.value.split("/");
var generatedDate = new Date(dateElements [2],dateElements [1]-1,dateElements [0]); 

Open in new window

0
 

Author Comment

by:djfenom
ID: 20410242
Excellent, knew it would be simple!

Thanks.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Make options hidden and show just option that is on 1 29
Start Auto-download of File when page loads? 1 21
Html Newline 7 20
Do alert on select 6 13
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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 …
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)
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…

856 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