473,472 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how can i use regular expressions to ensure a mysql format date entry in a text field?

how can i use regular expressions to ensure a mysql format date entry
in a text field?

thanks

marc

Oct 11 '06 #1
8 5357

libsfan01 wrote:
how can i use regular expressions to ensure a mysql format date entry
in a text field?
What is a "mysql format date entry"?
--
Rob

Oct 11 '06 #2
date in this format rob:

2006-03-02

regards

marc
RobG wrote:
libsfan01 wrote:
how can i use regular expressions to ensure a mysql format date entry
in a text field?

What is a "mysql format date entry"?
--
Rob
Oct 11 '06 #3
libsfan01 wrote:
how can i use regular expressions to ensure a mysql format date entry
in a text field?
I suppose you don't need years Ante Christum Natum or above 9999, which
aren't supported by MySQL's DATE type field. Please note that one- or
two-digit year notations are treated specially in MySQL
(0-69=>2000-2069 and 70-99=>1970-1999. Just use four digits to play
safe, eg 0006=>0006).

Note that the DATE field type in MySQL accepts Y(Y)(Y)(Y)-M(M)-D(D),
but doesn't actually check whether the date really exists up to day 31
(e.g. dates like 2006-2-31 are accepted).

Strictly the hyphens aren't even necessary, but I would counsel to keep
them in order to avoid all kinds of ambiguous situations.

Once you're aware of the above:

var D = '1941-8-19';
var S = D.split('-');

if ( /^\d{1,4}$/.test(S[0])
&& /^\d{1,2}$/.test(S[1])
&& 0 < parseFloat(S[1])
&& parseFloat(S[1]) < 13
&& /^\d{1,2}$/.test(S[2])
&& 0 < parseFloat(S[2])
&& parseFloat(S[2]) < 32
)
alert('date is OK to insert in MySQL');

Hope this helps,

--
Bart

Oct 11 '06 #4
doing it with regexp only and checking for Valid dates no 2006-2-29:

var CC = "((19)|(20))"; // valid centuries
// actually works for (1[7-9])|(2[0-3]) if you want to expand
century to 17-23
var Y29 = "(([02468][048])|([13579][26]))"; // years with 29 day in feb
var Y28 = "(([02468][1235679])|([13579][01345789]))"; // years with 28
days in feb
var M31 = "((0?[13578])|(1[02]))"; // months with 31 days
var M30 = "((0?[469])|(11))";// months with 30 days
var M2 = "(0?2)";// feb
var D31 = "((0?[1-9])|([12][0-9])|(3[01]))";// 31 days
var D30 = "((0?[1-9])|([12][0-9])|(30))";// 30 days
var D29 = "((0?[1-9])|([12][0-9]))"; // 29 days
var D28 = "((0?[1-9])|([12][0-8]))"; // 28 days
var sep = "-";// separator
var MonthsNotFeb = "(" + M31 + sep + D31 + ")|(" + M30 + sep+ D30 +
")";
var leapYears = Y29 + sep + "(" + MonthsNotFeb + "|(" + M2 + sep +
D29 + "))";
var nonleapYears = Y28 + sep + "(" + MonthsNotFeb + "|(" + M2 + sep +
D28 + "))";
var regDate = "^" + CC + "((" +leapYears + ")|(" + nonleapYears +
"))$";

// or the unreadable way:
var
regdate2='^((19)|(20))(((([02468][048])|([13579][26]))-((((0?[13578])|(1[02]))-((0?[1-9])|([12][0-9])|(3[01])))|(((0?[469])|(11))-((0?[1-9])|([12][0-9])|(30)))|((0?2)-((0?[1-9])|([12][0-9])))))|((([02468][1235679])|([13579][01345789]))-((((0?[13578])|(1[02]))-((0?[1-9])|([12][0-9])|(3[01])))|(((0?[469])|(11))-((0?[1-9])|([12][0-9])|(30)))|((0?2)-((0?[1-9])|([12][0-8]))))))$';
Bart Van der Donck wrote:
libsfan01 wrote:
how can i use regular expressions to ensure a mysql format date entry
in a text field?

I suppose you don't need years Ante Christum Natum or above 9999, which
aren't supported by MySQL's DATE type field. Please note that one- or
two-digit year notations are treated specially in MySQL
(0-69=>2000-2069 and 70-99=>1970-1999. Just use four digits to play
safe, eg 0006=>0006).

Note that the DATE field type in MySQL accepts Y(Y)(Y)(Y)-M(M)-D(D),
but doesn't actually check whether the date really exists up to day 31
(e.g. dates like 2006-2-31 are accepted).

Strictly the hyphens aren't even necessary, but I would counsel to keep
them in order to avoid all kinds of ambiguous situations.

Once you're aware of the above:

var D = '1941-8-19';
var S = D.split('-');

if ( /^\d{1,4}$/.test(S[0])
&& /^\d{1,2}$/.test(S[1])
&& 0 < parseFloat(S[1])
&& parseFloat(S[1]) < 13
&& /^\d{1,2}$/.test(S[2])
&& 0 < parseFloat(S[2])
&& parseFloat(S[2]) < 32
)
alert('date is OK to insert in MySQL');

Hope this helps,

--
Bart
Oct 11 '06 #5

br****@gmail.com wrote:
doing it with regexp only and checking for Valid dates no 2006-2-29:
Hmm... seems a bit over the top! :-)

To the OP:

Just about everything you ever needed to know about date validation is
here:

<URL: http://www.merlyn.demon.co.uk/js-date4.htm >

The trivial way that checks the format only, not validity, is:

var dateString = '2006-2-31';
alert( /\d{4}-\d\d?-\d\d?/.test(dateString)); // Shows true
For validation, a simple way is to convert the incoming date string to
a date object then test if the generated date matches the string - you
only need to test 2 of the 3 bits:

var dateString = '2006-02-31';

alert( /\d{4}-\d{2}-\d{2}/.test(dateString));

var dateBits = dateString.split('-');
var dateObj = new Date(dateString.replace(/-/g,'/'));
var isValid = ( dateBits[0] == dateObj.getFullYear()
&& dateBits[1] == (dateObj.getMonth()+1));
alert(isValid); // Shows false.
--
Rob

Oct 11 '06 #6

br****@gmail.com wrote:
doing it with regexp only and checking for Valid dates no 2006-2-29:
Hmm... seems a bit over the top! :-)

To the OP:

Just about everything you ever needed to know about date validation is
here:

<URL: http://www.merlyn.demon.co.uk/js-date4.htm >

The trivial way that checks the format only, not validity, is:

var dateString = '2006-2-31';
alert( /\d{4}-\d\d?-\d\d?/.test(dateString)); // Shows true
For validation, a simple way is to convert the incoming date string to
a date object then test if the generated date matches the string - you
only need to test 2 of the 3 bits:

var dateString = '2006-02-31';

alert( /\d{4}-\d{2}-\d{2}/.test(dateString));

var dateBits = dateString.split('-');
var dateObj = new Date(dateString.replace(/-/g,'/'));
var isValid = ( dateBits[0] == dateObj.getFullYear()
&& dateBits[1] == (dateObj.getMonth()+1));
alert(isValid); // Shows false.
--
Rob

Oct 11 '06 #7
RobG wrote:
Just about everything you ever needed to know about date validation is
here:

<URL: http://www.merlyn.demon.co.uk/js-date4.htm >

The trivial way that checks the format only, not validity, is:

var dateString = '2006-2-31';
alert( /\d{4}-\d\d?-\d\d?/.test(dateString)); // Shows true

For validation, a simple way is to convert the incoming date string to
a date object then test if the generated date matches the string - you
only need to test 2 of the 3 bits:

var dateString = '2006-02-31';

alert( /\d{4}-\d{2}-\d{2}/.test(dateString));

var dateBits = dateString.split('-');
var dateObj = new Date(dateString.replace(/-/g,'/'));
var isValid = ( dateBits[0] == dateObj.getFullYear()
&& dateBits[1] == (dateObj.getMonth()+1));
alert(isValid); // Shows false.
While this is absolutely useful for actual date validation, it's
irrelevant to the OP's question in a strict sense, which was checking
dates that are flagged okay to insert in a MySQL DATE field (which is a
significanctly different requirement).

You'ld be surprised what kind of entries MySQL accepts:
22121 =2022-12-01
100-05-21 =2010-00-05
0 =0000-00-00
etc.

Of course you have a point that it is in practice perhaps better to
just check for a valid date in the OP's case.

--
Bart

Oct 12 '06 #8
JRS: In article <11**********************@h48g2000cwc.googlegroups .com>,
dated Wed, 11 Oct 2006 14:20:28 remote, seen in
news:comp.lang.javascript, RobG <rg***@iinet.net.auposted :
>
For validation, a simple way is to convert the incoming date string to
a date object then test if the generated date matches the string - you
only need to test 2 of the 3 bits:

var dateString = '2006-02-31';

alert( /\d{4}-\d{2}-\d{2}/.test(dateString));
Matches '1234-56-789'.
var dateBits = dateString.split('-');
var dateObj = new Date(dateString.replace(/-/g,'/'));
var isValid = ( dateBits[0] == dateObj.getFullYear()
&& dateBits[1] == (dateObj.getMonth()+1));
alert(isValid); // Shows false.


If Y M D are arbitrary Numbers, then one need only test two of the three
parts. I'd not test the Year, but Month and Day in that order.

One can use .match instead of .test, saving a .split. Although .test
seems faster, .split seems slow (crudely tested).

If the Date is known to match \d{2} it is in 0..99, so any Date error
must give the wrong month (Date=365 generally gives the right month of the
wrong year); and a Month error also gives the wrong month. Therefore one
only needs to test the Month.

var dateString = '2006-02-31';

var M = dateString.match(/^(\d{4})-(\d{2})-(\d{2})$/)

alert(!!M)

var dateObj = new Date(dateString.replace(/-/g,'/'));
var isValid = M[2] == dateObj.getMonth()+1 // && M[3] == dateObj.getDate()
alert(isValid); // Shows false.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Oct 12 '06 #9

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

Similar topics

10
by: Phil Powell | last post by:
I need to do a very simple query in MySQL versions 3.23.41 and 4.0.10 whereby it would be something like this: $sql = "SELECT * FROM image WHERE image_path LIKE '%$imageLocationPath/+\$%'" ...
3
by: SROSeaner | last post by:
I am working on an ASP page that parses text using the VBScript.RegExp regular expression object. My reg expression right now is as follows: +\.+\.+/ And if find URL's no problem like: ...
4
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
1
by: Kerri | last post by:
Hi, I am new to .NET. I have a server side text box on an ASPX Page. I want to format the date when the user tabs out / double clicks teh textbox or presses carriage retutrn. What event do...
1
by: Daniel Walzenbach | last post by:
Hi, does anybody know I can extract a substring of a text with regular expressions. Let’s consider the following text: “Regular expressions are often used to make sure that a string matches a...
11
by: walterbyrd | last post by:
My MySQL table has a field that is set as type "date." I need to get today's date, and insert it into that field. The default for that MySQL field is 2006-00-00. I know about the date()...
3
by: Zeba | last post by:
Hi guys, I need some help regarding regular expressions. Consider the following statement : System.Text.RegularExpressions.Match match =...
2
by: ENBeginner | last post by:
Hello, I am working with Regular Expressions in Python. I have a text file (authors.txt) file that contains the first and last name of an author separated with a whitespace, then a whitespace...
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...
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
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,...
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...
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: 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 ...
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.