473,888 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegExp for Date

hello i have this function to check date (not mine)
function (s_date) {
// check format
if (!re_dt.test(s_ date))
return false;
// check allowed ranges
if (RegExp.$1 > 31 || RegExp.$2 > 12)
return false;
// check number of day in month
var dt_test = new Date(RegExp.$3, Number(RegExp.$ 2-1), RegExp.$1);
if (dt_test.getMon th() != Number(RegExp.$ 2-1))
return false;
return true;
}
it will check in this format DD-MM-YYYY
is it possible to modify it to be (MM/DD/YYYY OR DD/MM/YYYY) in one
function?

Jul 23 '05 #1
3 7070
zz*******@yahoo .com wrote:
hello i have this function to check date (not mine)
function (s_date) {
// check format
if (!re_dt.test(s_ date))
return false;
// check allowed ranges
if (RegExp.$1 > 31 || RegExp.$2 > 12)
return false;
// check number of day in month
var dt_test = new Date(RegExp.$3, Number(RegExp.$ 2-1), RegExp.$1);
if (dt_test.getMon th() != Number(RegExp.$ 2-1))
return false;
return true;
}
it will check in this format DD-MM-YYYY
is it possible to modify it to be (MM/DD/YYYY OR DD/MM/YYYY) in one
function?


How are we to know, you haven't shown what the actual regular exression
is. Presumably there is something somwhere that gives 're_dt' a value?


--
RobG
Jul 23 '05 #2
JRS: In article <11************ *********@o13g2 000cwo.googlegr oups.com>,
dated Fri, 27 May 2005 03:17:52, seen in news:comp.lang. javascript,
zz*******@yahoo .com posted :
hello i have this function to check date (not mine)
function (s_date) {
// check format
if (!re_dt.test(s_ date))
return false;
// check allowed ranges
if (RegExp.$1 > 31 || RegExp.$2 > 12)
return false;
// check number of day in month
var dt_test = new Date(RegExp.$3, Number(RegExp.$ 2-1),
RegExp.$1);
if (dt_test.getMon th() != Number(RegExp.$ 2-1))
return false;
return true;
}
it will check in this format DD-MM-YYYY
is it possible to modify it to be (MM/DD/YYYY OR DD/MM/YYYY) in one
function?


Yes. But neither DMY not MDY is good; YMD is how dates should be,
especially in data.

You should NOT in one input allow either DMY or MDY to be valid. One
can see that 25.12.2004 and 12/25/2005 are a year (and about 5000 miles)
apart - but is 01-07-2006, which is certainly valid, in Winter or in
Summer?

Therefore, your proposed modification would be too unwise to support.
There is no point in checking > 31 and > 12 as such; and you would, if
there were, also need to consider <=0.

The first 'return false' is probably unnecessary; if the final check is
judicious, it will catch soon enough what the first one would. But it
could be made to give a message "that does not even look like a date".

In js-date4.htm#DVal :-
function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
with (new Date(y, m, d))
return (getMonth()==m && getDate()==d) /* was y, m */ }
Read the newsgroup FAQ, especially sections 2.3 and 3.2.p (& 4.30).
<FAQENTRY> Could Sec 3.2 become an <ol type=a> list, please </FAQENTRY>
See below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3
Dr John Stockton wrote:
[...]
You should NOT in one input allow either DMY or MDY to be valid. One
can see that 25.12.2004 and 12/25/2005 are a year (and about 5000 miles)
apart - but is 01-07-2006, which is certainly valid, in Winter or in
Summer?


For me at least, 01-07-2006 will be winter (global warming ignored).
It will also likely be winter in that place that persists with M-D-Y
format, but 6 months earlier. :-)

Of course those who share the same hemisphere will disagree.

[...]

--
Rob
Jul 23 '05 #4

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

Similar topics

1
5329
by: Lynn | last post by:
On a form I have Date_Start Date_End I have a new Date_Start1 Date_End1 which the use inputs. I need to validate that Date_Start1 and Date_End1 are a date range entirely separate from Date_Start and Date_End i.e. no overlapping
14
13276
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 am expecting. Can anyone help please TIA Steve PS I'm new to javascript
2
1587
by: mirza i | last post by:
thanks for the previous replies. here is the new question (i'm absolutely sure that this should be VERY easy for a good js coder) ok: from asp i call: <img src="pics/cal.gif" onclick="show_calendar_det('document.invdetdet.dx1',
4
3571
by: mike | last post by:
I have a data like: 03-Jul-05 and need to convert it to: 7/3/2005 Is there a function for this already?
6
4844
by: Jim Davis | last post by:
Before I reinvent the wheel I thought I'd ask: anybody got a code snippet that will convert the common ISO8601 date formats to a JS date? By "common" I mean at the least ones described in this W3C note: http://www.w3.org/TR/NOTE-datetime For my requirements the code would be need to be open sourceable under the BSD license.
5
4593
by: Bob Sanderson | last post by:
I would like to use a date picker on a web page input form. I found one which does what I want but the date format it outputs is not correct for my form. The script contains the following: // datetime parsing and formatting routimes. modify them if you wish other datetime format function str2dt (str_datetime) { var re_date = /^(\d+)\-(\d+)\-(\d+)\s+(\d+)\:(\d+)\:(\d+)$/; if (!re_date.exec(str_datetime)) return alert("Invalid Datetime...
32
5044
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is not a function" ---------------------------------------------------- I have this in a .js file and in the head section.
17
5296
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
3
7237
by: samuelberthelot | last post by:
Hello, I would like to validate a date in a textbox on the onChange event. The date must be in the format 01/01/2007 I would like to use a regular expression to validate it but I'm not very familiar with them. How can I do it ? Thanks
0
9961
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9800
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11178
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10777
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7148
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5817
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
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 we have to send another system
2
4243
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3251
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.