473,473 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

regex on date

Ike
If I am requiring a date be entered as YYYY-MM-DD.....why then....

var validformat=/^\d{4}\[-]\d{2}[-]\d{2}$/ //Basic check for format validity

if (!validformat.test(input.value))
//I come here for 2006-11-10.....why?

Thanks, Ike
Oct 9 '06 #1
3 2741


Ike wrote:
If I am requiring a date be entered as YYYY-MM-DD.....why then....

var validformat=/^\d{4}\[-]\d{2}[-]\d{2}$/ //Basic check for format validity
I think you want
/^\d{4}\-\d{2}\-\d{2}$/

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 9 '06 #2
Martin Honnen wrote on 09 okt 2006 in comp.lang.javascript:
>

Ike wrote:
>If I am requiring a date be entered as YYYY-MM-DD.....why then....

var validformat=/^\d{4}\[-]\d{2}[-]\d{2}$/ //Basic check for format
validity

I think you want
/^\d{4}\-\d{2}\-\d{2}$/
no need for eacaping the - outside [].

shorter:

/^\d{4}(-\d{2}){2}$/

/^\d{4}(-\d\d){2}$/

one could test less:

/^\d{4}.{6}$/

or more:

/^[12]\d{3}-[01]\d-[0123]\d$/

or even more:

/^[12]\d{3}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$/

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 9 '06 #3
JRS: In article <45***********************@newsspool2.arcor-online.net>,
dated Mon, 9 Oct 2006 18:51:37 remote, seen in
news:comp.lang.javascript, Martin Honnen <ma*******@yahoo.deposted :
>Ike wrote:
>If I am requiring a date be entered as YYYY-MM-DD.....why then....

var validformat=/^\d{4}\[-]\d{2}[-]\d{2}$/ //Basic check for format validity
^^ ^ ^ ^ superfluous
| error
>
I think you want
/^\d{4}\-\d{2}\-\d{2}$/
/^\d{4}-\d\d-\d\d$/ is adequate.
In the past, the newsgroup has accepted

nD = new Date(Y, M-1, D)
OK = (nD.getMonth()+1 == M) && (nD.getDate() == D)

as the (joint?) best way to validate Y M D with a Date Object.

It occurs to me that if one has used Mt = String.match(RegExp) to
check the pattern of the input string, and D = +Mt[3] etc., then one
only needs
OK = nD.getMonth()+1 == M

OK ?

--
© 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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 9 '06 #4

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

Similar topics

5
by: Ali Eghtebas | last post by:
Hi, I've made this regex to catch the start of a valid multiline comment such as "/*" in e.g. T-SQL code. "(?<=^(?:*'*')*?*)(?<!^(?:*'*')*?--.*)/\*.*?$" With Multiline option on. As we know...
8
by: rjb | last post by:
Hi! Could somebody have a look and help me to optimize the code below. It may look like very bad way of coding, but this stuff is very, very new for me. I've included just few lines. Regex...
3
by: Francesco | last post by:
Hi to all, someone of you has a regular expression for image file in Asp.NET C# ? Thanks Francesco
4
by: | last post by:
Here is an interesting one. Running asp.net 2.0 beta 2. I have a regular expression used in a regex validator that works on the client side in Firefox but not in IE. Any ideas? IE always reports...
1
by: gs | last post by:
is it OK to get regex result of pattern which has duplicate reference names like StrDatePattern As String ="\b(((?<year>\d{4})-(?<month>\d{1,2})-(?<day>\d{2}))|{(?<month>\d{1,2}) (?<day>\d{2}),...
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
5
by: shapper | last post by:
Hello, I am trying to create a RegEx which validates dates in the following format: dd/mm/yyyy 27/12/2007, 02/03/2006 d/m/yy 1/4/97, 2/3/06 Basically: - Day can be 1 to 31...
5
by: shapper | last post by:
Hello, What is the Regex expression to validate a date time format as follows: dd-mm-yyyy hh:mm:ss An example: 20-10-2008 10:32:45
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
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...
0
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...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.