Solved

Date validation script

Posted on 2007-11-16
9
623 Views
Last Modified: 2012-06-21
I have a form with two date fields: startdate & enddate.  The dates are selected using a javascript popup calendar so I'm not worried about the format but I need to validate three things:

1. Both fields must be completed.
2. Dates cannot be in the past.
3. The enddate cannot be before the startdate.

The dates are in the following format: %d-%b-%Y (16-Nov-2007)
0
Comment
Question by:Orroland
  • 6
  • 2
9 Comments
 
LVL 3

Expert Comment

by:samalraj
ID: 20297128
hi,
    check this link it may help you
http://www.ozzu.com/ftopic46552.html

Regards,
Amal
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20297263
<form onSubmit="return validate(this)">
<script>

function validate(theForm) {

  var startDateString = theForm.startdate.value;

  if (startDateString=="") {

    alert('Please fill in start date');

    theForm.startDate.focus();

    return false;

  }

  var endDateString = theForm.enddate.value;

  if (endDateString=="") {

    alert('Please fill in end date');

    theForm.endDate.focus();

    return false;

  }

  var startDate = new Date(Date.parse(startDateString.replace(/-/g,' ')));

  var endDate = new Date(Date.parse(endDateString.replace(/-/g,' ')));

  var today = new Date();

  if (startDate < today) {

    alert('Start date cannot be in the past');

    theForm.startdate.focus();

    return false;

  }

  if (endDate < today) {

    alert('End date cannot be in the past');

    theForm.enddate.focus();

    return false;

  }

  if (startDate > endDate) {

    alert('Start date cannot be greater than end date');

    theForm.startdate.focus();

    return false;

  }

  return true

}  

</script>

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 20297320
Sorry... A few case issues and a resetting of hours, minutes and seconds were needed
<script>

function validate(theForm) {

  var startDateString = theForm.startdate.value;

  if (startDateString=="") {

    alert('Please fill in start date');

    theForm.startdate.focus();

    return false;

  }

  var endDateString = theForm.enddate.value;

  if (endDateString=="") {

    alert('Please fill in end date');

    theForm.enddate.focus();

    return false;

  }

  var startDate = new Date(Date.parse(startDateString.replace(/-/g,' ')))

  var endDate = new Date(Date.parse(endDateString.replace(/-/g,' ')))

  var today = new Date();

  today.setHours(0,0,0,0)

  if (startDate < today) {

    alert('Start date cannot be in the past');

    theForm.startdate.focus();

    return false;

  }

  if (endDate < today) {

    alert('End date cannot be in the past');

    theForm.enddate.focus();

    return false;

  }

  if (startDate > endDate) {

    alert('Start date cannot be greater than end date');

    theForm.startdate.focus();

    return false;

  }

  return true

}  

</script>

<form onSubmit="return validate(this)">

<input type="text" name="startdate" value="15-Nov-2007"><br>

<input type="text" name="enddate" value="14-Nov-2007"><br>

<input type="submit">

</form>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20297331
@samalraj none of the examples in that page dealt with DD mmm YYYY formats
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:newjeep19
ID: 34249061
How can you change the code above to were the enddate can not be greater than 6 months 180 days from the startdate?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34249237
Something like this
<script>

function validate(theForm) {

  var startDateString = theForm.startdate.value;

  if (startDateString=="") {

    alert('Please fill in start date');

    theForm.startdate.focus();

    return false;

  }

  var endDateString = theForm.enddate.value;

  if (endDateString=="") {

    alert('Please fill in end date');

    theForm.enddate.focus();

    return false;

  }

  var startDate = new Date(Date.parse(startDateString.replace(/-/g,' ')))

  var endDate = new Date(Date.parse(endDateString.replace(/-/g,' ')))

  var today = new Date();

  today.setHours(0,0,0,0)

  if (startDate < today) {

    alert('Start date cannot be in the past');

    theForm.startdate.focus();

    return false;

  }

  if (endDate < today) {

    alert('End date cannot be in the past');

    theForm.enddate.focus();

    return false;

  }

  if (startDate > endDate) {

    alert('Start date cannot be greater than end date');

    theForm.startdate.focus();

    return false;

  }

  today.setMonth(today.getMonth()+6);

  if (endDate > today) {

    alert('End date cannot be greater than 6 months from now');

    theForm.enddate.focus();

    return false;

  }

  return true

}  

</script>

<form onSubmit="return validate(this)">

<input type="text" name="startdate" value="1-Dec-2010"><br>

<input type="text" name="enddate" value="14-Dec-2010"><br>

<input type="submit">

</form>

Open in new window

0
 

Expert Comment

by:newjeep19
ID: 34249770
OK I am trying to use this with my current form
my  asp.net code:
<asp:TextBox ID="startdate" runat="server"></asp:TextBox><br/>
                                                     <h5 style="font-family: Arial; text-align: left;">End Date:</h5>
                                                     <asp:TextBox ID="enddate" runat="server"></asp:TextBox><br />
                                                     <input type="submit"/>
I get this error when running your javascript code:
Microsoft JScript runtime error: 'startdate.value' is null or not an object
Microsoft JScript runtime error: 'enddate.value' is null or not an object
Microsoft JScript runtime error: 'undefined' is null or not an object (show's 2x's)
How can I take your validator (that works great and plug it into my Asp.Net code
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34249903
Please ask that in the asp zone
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34250259
You likely need enddate.ClientID or something
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

912 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

23 Experts available now in Live!

Get 1:1 Help Now