|
|
Home » Javascript » Article
Simple date validation
|
| Article by: | Chris Hogben (11/26/2003) |
|
| Summary: | function that takes a date in three parts, day, month and year - returns true if it's a valid date. |
|
| Viewed: 129097 times |
Rating (54 votes): |
|
4.3 out of 5 |
|
|
|
Simple date validation
Very simple, handles leaps years.
The month needs to be a 0 (zero) for Jan, upto 11 to Dec.
function isValidDate(day,month,year){
/*
Purpose: return true if the date is valid, false otherwise
Arguments: day integer representing day of month
month integer representing month of year
year integer representing year
Variables: dteDate - date object
*/
var dteDate;
//set up a Date object based on the day, month and year arguments
//javascript months start at 0 (0-11 instead of 1-12)
dteDate=new Date(year,month,day);
/*
Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. We'll use this to our advantage by creating the date object and then comparing it to the details we put it. If the Date object is different, then it must have been an invalid date to start with...
*/
return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}
|
|
View highlighted Comments
User Comments on 'Simple date validation'
|
Posted by :
Jen1234x at 04:19 on Thursday, May 20, 2004
|
I saw several people asking for help in find the location of an IP number so I thought I'd try the same.
Can you please tell me in which STATE these IP numbers originated from? Meaning where does the person live who is assigned these numbers?
The IP numbers are 67.232.135.57......this is a netscape address.
205.188.157.36...........this is an aol address.
Thanks!
Jen
Oh, sorry! I didn't comment on the above post. I used these space because I don't know how to send a post otherwise. I will say this though about the above post. It is very complicated for someone who doesn't know things like that. Good going. I admire those who have websites and know all the in's and out's.
| |
Posted by :
emorgan at 10:21 on Friday, April 01, 2005
|
Found this after finding out javascript doesnt except invalid dates when trying to create a new object, was quite gobsmacked it just created the next valid date, must be loads of bugs out there from this.
extremely simple good logic
Cheers
| |
Posted by :
Sittan at 15:23 on Wednesday, June 08, 2005
|
Hello,
I used this function on
06/08/2005
and it didn't work
it returned false
<Added>
Sorry,
It was my mistake.
The function works greate.
Sittan
| |
Posted by :
kang at 03:12 on Friday, September 22, 2006
|
I've seen lots of date validation codes.
they checked number, leap year, or used regex etc...
but yours are the best.
Simple, so great.
Thanks.
| |
Posted by :
Altori at 13:48 on Wednesday, November 15, 2006
|
This is great. I needed to get the day month and year parts out of a single string so I have created the following by combining another script I found:
function isValidDate(strDate){
var dteDate;
var day, month, year;
var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
var matchArray = strDate.match(datePat);
if (matchArray == null)
return false;
day = matchArray[1]; // p@rse date into variables
month = matchArray[3];
year = matchArray[5];
month--;
dteDate=new Date(year,month,day);
return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}
| |
Posted by :
mk_murthy at 09:36 on Wednesday, September 23, 2009
|
Sir,
Please give java code for dateformat()
Thanks.,
| |
|
To post comments you need to become a member. If you are already a member, please log in .
| RELATED ARTICLES |
Javascript - Enable and Disable form elements by Jeff Anderson
This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form. |
 |
Check IsNumeric Function by Jeff Anderson
A javascript validation function to check whether the details entered by a user are numeric. |
 |
Javascript Onload Event by Jeff Anderson
Sometimes you need to perform an action immediatley after the page has loaded. That's when the onLoad Event Handler comes in handy |
 |
Form Validation Function by Jeff Anderson
A javascript validation function that you can use to validate all types of forms. |
 |
JavaScript Field Is Empty Form Validation by Jeff Anderson
This javascript function allows you to check whether a form field has been completed or not. |
 |
Javascript Get Selected Text by Jeff Anderson
A cross-browser script to get text selected by the user |
 |
Check Email Validation Function by Jeff Anderson
A javascript validation function to check whether the user has entered a valid email address in a form. |
 |
Multiple submit buttons on a single form by Kiran Pai
This script shows you how to submit the contents of a form to different programs depending on which Submit button you press. Additionally it also shows how to call two different functions when you press the Submit button. |
 |
Validate Form and Disable Submit Button by Marylou Marks
I have a form that validates info, but I also want to disable the submit button. The disable part worked before adding the form validating. |
 |
Javascript onUnload Event Handler by Jeff Anderson
The onUnload Event Handler allows you to perform an action as the user leaves the page. |
 |
| |