473,378 Members | 1,375 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,378 software developers and data experts.

Date format dd/mm

Ian
I would like to have some validation on a date field. The date format
is dd/mm which is used for our financial year end. I suppose I need
also consider leap years. Please can you shed some light on how I will
be able to achieve this?
Jul 23 '05 #1
2 2167
On 8 Oct 2004 01:32:28 -0700, Ian <is****@cafonline.org> wrote:
I would like to have some validation on a date field. The date format is
dd/mm which is used for our financial year end. I suppose I need also
consider leap years. Please can you shed some light on how I will be
able to achieve this?


The easiest way is to validate the format of the date, then use the
built-in Date object to verify the actual numbers are valid. This is
performed by creating a Date object with the given values, then check that
you get those numbers back (the Date object corrects dates automatically,
so the month and day won't match with out-of-range numbers).

The only question I must ask is how is the year determined? Is it always
the current year or does it differ? I've assumed the current year in the
code below. I've also heavily commented the code so you can see what's
going on.

<!-- Change the id values as you wish. Also remember the name
-- attribute if you intend to submit the value to a server.
-->
<label for="myDate">Date (dd/mm):
<input id="myDate" size="5" onchange="checkDate(this);">
</label>
function checkDate(obj) {
/* Returns an array that contains three elements if the date is
* valid, or null if invalid.
* 0 - The value that matches the regular expression
* 1 - The day
* 2 - The month
*/
var valid = /^([0123]\d)\/([01]\d)$/.exec(obj.value);
/* If the return value is an array, it will be type-converted to
* boolean true. The value, null, is type-converted to false.
*/
if(valid) {
/* Create a new Date object, initialised to the current date.
* This automatically provides the current year. Also, convert
* the user input to numbers using unary plus (+).
* Note that month values start at zero, not one (hence the -1).
*/
var date = new Date(),
day = +valid[1],
month = +valid[2] - 1;
/* Change the date object to reflect the user input. */
date.setMonth(month, day);
/* Compare the values returned by the Date object with those
* provided by the user. If they match, the input is valid.
*/
valid = (date.getDate() == day) && (date.getMonth() == month);
}
/* This part is optional. If the value is not valid, alert the
* user.
*/
if(!valid) {
alert('Please enter a valid date.');
obj.focus();
}
/* This is also optional. If you use this function in a longer
* validation sequence, such as one that might be used when the
* form submits, this return signals whether the value was in
* fact, valid.
*/
return valid;
}

Partially tested.

This example use the change event for the INPUT control. The value should
also be examined when the form is submitted using the FORM's submit event
and, of course, on the server.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
JRS: In article <opsfjt2xg4x13kvk@atlantis>, dated Fri, 8 Oct 2004
11:25:43, seen in news:comp.lang.javascript, Michael Winter <M.Winter@bl
ueyonder.co.invalid> posted :
On 8 Oct 2004 01:32:28 -0700, Ian <is****@cafonline.org> wrote:
I would like to have some validation on a date field. The date format is
dd/mm which is used for our financial year end. I suppose I need also
consider leap years. Please can you shed some light on how I will be
able to achieve this?

Best by reading the newsgroup FAQ and seeing what it says adjacent to
the words "date" / "dates". Or by following sig line 3 below.

As you give no location, one cannot guess when your FY ends; it will be,
I suppose, either this year or next year - or you may already know the
end year. If you are US, test state & month for year end; if you are
UK, test Month*100 + Date against 405 or 406; otherwise ... (actually,
the FYs of organisations do not always match those of their
surroundings; for example, IIRC, most US states do not match the Feds).

The easiest way is to validate the format of the date, then use the
built-in Date object to verify the actual numbers are valid.
Agreed.
...
var valid = /^([0123]\d)\/([01]\d)$/.exec(obj.value);
No need to check digit values here, though.
/^(\d\d)\D+(\d\d)$/ or /^(\d+)\D+(\d+)$/ should do, if
liberal data entry is wanted.
date.setMonth(month, day);
Possibly worth noting the importance of doing that in a single step,
since doing it in two steps will often, but not always, work, depending
on the current date & the date given.

Partially tested.


The fundamentally similar code cited can probably be considered well
tested, so yours should work!

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
2
by: amith | last post by:
hi I have written javascript for comparing two dates in US format and finding out whether the start date is greater than the end date and vice versa. In this attempt i have instantiated the...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
3
by: Lyn | last post by:
Hi, I am developing a project in which I am checking for records with overlapping start/end dates. Record dates must not overlap date of birth, date of death, be in the future, and must not...
5
by: Macca | last post by:
Hi, I have a table which has a date/time field. I am storing them as follows :- 01/01/2005 11:25 01/01/2005 19:44 02/01/2005 05:04
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
20
by: andreas | last post by:
When I copy a vb.net project using date formats from one PC with a windows date format f.e. dd/mm/yyyy to another PC having a format yy/mm/dd then I get errors. How can I change for a while in the...
30
by: fniles | last post by:
On my machine in the office I change the computer setting to English (UK) so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US. This problem happens in either Access or SQL Server. In the...
4
by: OzNet | last post by:
I have some functions to calculate the working days in a given period. This includes a table that is queried to calculate the number of public holidays that don’t occur on a weekend. If I test...
11
ollyb303
by: ollyb303 | last post by:
Hello, I am using a dynamic crosstab report to track performance statistics for my company and I have hit a problem. I would like the option to track stats daily (for the last 7 complete...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.