473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

JavaScript function to check date field for future date

Greetings,

I have an ASP page with a form (form1) that contains a JavaScript validation function that is activated onSubmit. The function contains a series of IF statements to alert the user to any blanks contained in the form elements.

What I want to do is add a new IF statement to the function that checks whether the user has populated a date field (entry_date01) with a date that is in the future. In other words, the user can input the current date (or even a past date) but not a future date.

I know some JavaScript but not enough to code the above function. I would appreciate anyone's help in accomplishing this task.

Regards,

JM
Sep 17 '08 #1
7 52185
acoder
16,027 Expert Mod 8TB
Take the date input and convert it into a Date object. Create another new Date object (which defaults to the current date/time) and compare the two. See example.
Sep 17 '08 #2
I understand the concept, but I'm not sure how to write the code to convert the date input into a Date object.

To practice, I created a simple ASP page with a form that contains one text field and a submit button. For simplicity's sake, the text field value is initially populated with today's date.

I wrote two JavaScript functions to be activated onSubmit: (1) a check for a null value in the text field, and (2) the date conversion and comparison. The first function works fine, but there's something with the 2nd function I'm missing and it's probably the conversion code.

Here's what I have so far:

Expand|Select|Wrap|Line Numbers
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Untitled Document</title>
  7.  
  8. <script type="text/javascript">
  9. function checkform()
  10. {
  11.      if (document.form1.entrydate01.value == "")
  12.      { 
  13.       //something is wrong
  14.       alert('REQUIRED FIELD ERROR: Please enter date in field!')
  15.       return false;
  16.       }
  17.       // if script gets this far through all of your fields
  18.       // without problems, it's ok and you can submit the form
  19.       return true;
  20.      }
  21. </script>
  22.  
  23. <script type="text/javascript">
  24. function checkdate()
  25. {
  26. var myDate = new Date(document.form1.entrydate01.value);
  27. var today = new Date();
  28. {
  29.     if (myDate>today)
  30.     {
  31.     alert('You cannot enter a date in the future!')
  32.     return false;
  33.      }
  34.     else alert('This is a valid date')
  35.     return true;
  36. }
  37. }
  38. </script>
  39. </head>
  40. <body>
  41.  
  42. <form action="" method="post" name="form1" id="form1" onsubmit="return checkform();return checkdate()">
  43.   <label>
  44.   <input type="text" name="entrydate01" id="entrydate01" value="<%= FormatDateTime(Date, 0)%>"/>
  45.   </label>
  46.   <label>
  47.   <input type="submit" name="Submit" id="Submit" value="Submit" />
  48.   </label>
  49. </form>
  50.  
  51. </body>
  52. </html>
Any suggestions?
Sep 18 '08 #3
acoder
16,027 Expert Mod 8TB
The reason it doesn't work is that you're returning with "return checkform()", so checkdate() never gets called. The easiest solution is to call checkdate within checkform.
Sep 18 '08 #4
Okay. I corrected that but it's still not working right. Now when I click the submit button I get an IE error on the page for line 9 that says, "document.form1.entrydate01 is null or not an object". I get this error even when there's a date in the text box

Here's the updated code:

Expand|Select|Wrap|Line Numbers
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Untitled Document</title>
  7.  
  8. <script type="text/javascript">
  9. var dateString = document.form1.entrydate01.value;
  10. var myDate = new Date(dateString);
  11. var today = new Date();
  12.  
  13. function checkform()
  14. {
  15.      if (document.form1.entrydate01.value == "")
  16.      { 
  17.       //something is wrong
  18.       alert('REQUIRED FIELD ERROR: Please enter date in field!')
  19.       return false;
  20.       }
  21.       else if (myDate>today)
  22.       { 
  23.       //something else is wrong
  24.         alert('You cannot enter a date in the future!')
  25.         return false;
  26.       }
  27.       // if script gets this far through all of your fields
  28.       // without problems, it's ok and you can submit the form
  29.       return true;
  30.      }
  31. </script>
  32. </head>
  33. <body>
  34.  
  35. <form action="" method="post" name="form1" id="form1" onsubmit="return checkform()">
  36.   <label>
  37.   <input type="text" name="entrydate01" id="entrydate01" value="<%= FormatDateTime(Date, 0)%>"/>
  38.   </label>
  39.   <label>
  40.   <input type="submit" name="Submit" id="Submit" value="Submit" />
  41.   </label>
  42. </form>
  43.  
  44. </body>
  45. </html>
Sep 19 '08 #5
acoder
16,027 Expert Mod 8TB
Lines 9-11 should be inside the checkform() function.
Sep 19 '08 #6
That did it! It's usually something simple that I overlook.

Thanks, acoder!

JM
Sep 19 '08 #7
acoder
16,027 Expert Mod 8TB
You're welcome. Glad it's working :)
Sep 19 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
2
by: asreryll | last post by:
I wish to show a future date in a table, so that I can sort and know which case is in need of a review. I can display a future date in a form but I want to see all records that are due on a certain...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
1
by: CZ | last post by:
Hello, I have a list of employees with their birhtdays. I would like to to have a datagrid list employees with birthdays on the current month on a web page. I tried select * from tbldate...
6
by: rohayre | last post by:
Im a long time java developer and actually have never done anything with java scripting. I'd like to write a short simple script for calculating a date in the future based on today's date and a...
2
by: sudhashekhar30 | last post by:
hi i have done dis b4.but now it's not working.don't no why?can any one help why it's so? javascript code is in content page like dis--- javascript in html page:- <script...
2
by: =?Utf-8?B?Sm9ubnk=?= | last post by:
I have an ASP.NET 2.0 C# web application that is contacting an Exchange server using WEBDAV. It allows the users to look up appointments for a future date. The problem I have is determining the...
1
by: dkyadav80 | last post by:
Hi Sir, I'm begener in Ajax or Javascript.I have problem about date validation. I have 3 field in html page: 1->date(numeric), 2->month(text 1st 3 letter) 3->year(numeric). I wanna implement to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.