473,467 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Date validation not working properly

24 New Member
I want a date field to be validated and this is my code:
It has From date & To date:

Expand|Select|Wrap|Line Numbers
  1. <script>function checkdate(frmdt,todt){
  2.     var validformat=/^\d{2}\-\d{2}\-\d{4}$/
  3.     var returnval=false
  4.        if(!validformat.test(frmdt.value)){
  5.            alert("Invalid frmdt");
  6.            document.form.frmdt.value="";
  7.    }
  8.        else if(!validformat.test(todt.value)){
  9.        alert("Invalid Date 2");
  10.         document.form.todt.value="";
  11.    }
  12.         else{
  13.         var start = document.form.frmdt.value;
  14.          var end = document.form.todt.value;
  15.  
  16.          var stDate = new Date(start);
  17.         var enDate = new Date(end);
  18.        var compDate = enDate - stDate;
  19.  
  20.         if(compDate >= 0)
  21.        return true;
  22.          else
  23.             {
  24.           alert("End date should be greater than start date.");
  25.           return false;
  26.          }
  27.            }
  28.          }</script>
But this code accepts every characters on the keyboard and gives a popup "error" accepting the characters...

It also gives error even when proper date is entered..

For eg if I enter 1, it doesnot wait for me to enter next no. and displays error while accepting 1. It does this with every character.

I cant figure out what is wrong in the code.
May 9 '13 #1
5 1275
Rabbit
12,516 Recognized Expert Moderator MVP
We would need to see how the code is called.
May 9 '13 #2
Shepard
24 New Member
This is how i defined my field
Expand|Select|Wrap|Line Numbers
  1. <input id="date" type="text" onkeydown="return checkdate(frmdt,todt)" maxlength="10">
May 10 '13 #3
Rabbit
12,516 Recognized Expert Moderator MVP
That's why it's running on every key press. Because you used the onkeydown event. That fires every time a key is pressed. So it's going to check the first time you hit a key. You're probably looking for the onblur event or something similar. onblur only fires when the user leaves the control.

There are also probably other problems but that's the first one we need to take care of before we know if there are others.
May 10 '13 #4
Shepard
24 New Member
i tried onblur,onkeyup,onchange,etc.. only onkeypress and onkeyup atleast give an error for every character.

Rest don't give any error and accepts everything.
May 10 '13 #5
Rabbit
12,516 Recognized Expert Moderator MVP
That's because there's more errors in the code. But you first need to pick the right event. Most likely either onblur or onchange.

After fixing that error, the next one you will need to address is how you reference the html elements. I don't believe you can access it that way, you will most likely need to use the getElementById method.

After that error, we will need to see what other errors come up.
May 10 '13 #6

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

Similar topics

30
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
14
by: Steve Wright | last post by:
I am trying to validate if an entry on a form is a date. I have adapted code I found here http://www.codingforums.com/archive/index.php/t-14325 as below but I can't seem the get the results that I...
11
by: Diego | last post by:
Hi all a quick question: how can I validate a date in asp.net (2.0) with c#? I didn't find a quick anwer. Thanks, Diego.
5
by: Nita Raju | last post by:
Hi, I have to validate a textbox for date without using the validation controls. So i had to use IsDate(). It's not working properly when i give "11//2004". When i enter the above date it...
3
by: pmarisole | last post by:
The following javascript code gives me the date validation that I need except after the correct date is entered into the field, it puts the date in the wrong format EXAMPLE: User enters...
2
by: John Smith | last post by:
Hello, I have a VB.NET application with a Windows form that have several textboxes fields where I have dates entered. I would like to do a date validation check after the the field is updated, so...
8
pradeepjain
by: pradeepjain | last post by:
<script language = "Javascript"> /** * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/) */ // Declaring valid date character, minimum year and maximum...
5
Stang02GT
by: Stang02GT | last post by:
I have been asked to validate a date on our web-page so that people cannot enter dates like 14/1/08 or 2/30/06. I have found code that will do exactly what i need it to do, but i am not sure how to...
2
by: mshroom12 | last post by:
I am having trouble with the following project on hand. I use Eclipse to do my work in Java. This is what I'm supposed to complete. Date Validation In this exercise you will write a program...
1
by: robin1983 | last post by:
Dear All, I got stuck in simple problem, I have a two php file one for registration form and one for to check and insert into the table. The problem is that when I get any kind error in...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.