Link to home
Start Free TrialLog in
Avatar of Orroland
OrrolandFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Date validation script

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)
Avatar of samalraj
samalraj
Flag of India image

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

Regards,
Amal
Avatar of Michel Plungjan
<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

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark 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
@samalraj none of the examples in that page dealt with DD mmm YYYY formats
How can you change the code above to were the enddate can not be greater than 6 months 180 days from the startdate?
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

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
Please ask that in the asp zone
You likely need enddate.ClientID or something