html form: date field auto-adjust

M

Martin Herrman

Dear scripters,

I am working on a HTML form in which a date must be entered of the form
'dd-mm-yyyy'. Now I'm looking for a script that, when the user switches to
another form field, changes e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.

I have searched the net using google, but only found validators that show
an error if an incorrect format is used. (and I can't write one on my own)

Does someone has such a script for me?

much thanks in advance!

Martin
 
L

Lee

Martin Herrman said:
Dear scripters,

I am working on a HTML form in which a date must be entered of the form
'dd-mm-yyyy'. Now I'm looking for a script that, when the user switches to
another form field, changes e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.

I have searched the net using google, but only found validators that show
an error if an incorrect format is used. (and I can't write one on my own)

That can't be done, because the code can't guess whether 1122005 is January 12
or November 2. That's why you have to ask the user to correct it.
 
M

McKirahan

Lee said:
Martin Herrman said: own)

That can't be done, because the code can't guess whether 1122005 is January 12
or November 2. That's why you have to ask the user to correct it.

Unless you only reformat the entry with dashes if it has a length of 8.

<html>
<head>
<title>ddmmyyyy.htm</title>
<script type="text/javascript">
function format(that) {
var what = that.value;
if (what.length != 8) return;
that.value = what.substr(0,2) + "-" + what.substr(2,2) + "-" +
what.substr(4);
}
</script>
</head>
<body>
<form>
<input type="text" name="dmy" size="10" maxlength="10"
value="01122005" onblur="format(this)">
</form>
</body>
</html>
 
E

Evertjan.

Lee wrote on 15 mrt 2005 in comp.lang.javascript:
Martin Herrman said:

That can't be done, because the code can't guess whether 1122005 is
January 12 or November 2. That's why you have to ask the user to
correct it.

'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.

Given these constraints and
the boundaries of the "current" century
it can be done.

function organize(x){
if (/^\d{8}$/.test(x))
x.value = x.replace(/(\d{2})(\d{2})(\d{4})/,'$1-$2-$3')
else if (/^\d{1,2}\d{1,2}\d{2}$/.test(x)) {
a = x.split('-')
x.value = two(a[0])+'-'+two(a[1])+
'-'+(+a[2]>30)?'19'+a[2]:'20'+a[2]
}
}

function two(x){
return (x<10)?'0'+x:x
}

...............

<input onblur='organize(this)'>
 
E

Evertjan.

Evertjan. wrote on 15 mrt 2005 in comp.lang.javascript:
'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.

Given these constraints and
the boundaries of the "current" century
it can be done.

function organize(x){
if (/^\d{8}$/.test(x))
x.value = x.replace(/(\d{2})(\d{2})(\d{4})/,'$1-$2-$3')
else if (/^\d{1,2}\d{1,2}\d{2}$/.test(x)) {

else if (/^\d{1,2}-\d{1,2}-\d{2}$/.test(x)) {
a = x.split('-')
x.value = two(a[0])+'-'+two(a[1])+
'-'+(+a[2]>30)?'19'+a[2]:'20'+a[2]
}
}

function two(x){
return (x<10)?'0'+x:x
}
 
L

Lee

Evertjan. said:
Lee wrote on 15 mrt 2005 in comp.lang.javascript:


'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.

Given these constraints and
the boundaries of the "current" century
it can be done.

But you can't assume those constraints.
 
M

McKirahan

Lee said:
Martin Herrman said: own)

That can't be done, because the code can't guess whether 1122005 is January 12
or November 2. That's why you have to ask the user to correct it.

"... e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'." doesn't include
'd-mm-yyy';
therefore, '1122005' is November 2, 2005.

Unless the examples ("e.g.") were not definitive.
 
E

Evertjan.

Lee wrote on 15 mrt 2005 in comp.lang.javascript:
But you can't assume those constraints.

They are given by the OP.

Why improving on a reasonable request,
instead of showing the asked code?
 
Z

Zifud

McKirahan wrote:
[...]
"... e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'." doesn't include
'd-mm-yyy';
therefore, '1122005' is November 2, 2005.

Based on the suggested format, I make it 1 December 2005, or it
may be 11 February 2005.

Or does ddmmyyyy really mean mmddyyyy?
 
M

McKirahan

Zifud said:
McKirahan wrote:
[...]
"... e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'." doesn't include
'd-mm-yyy';
therefore, '1122005' is November 2, 2005.

Based on the suggested format, I make it 1 December 2005, or it
may be 11 February 2005.

Or does ddmmyyyy really mean mmddyyyy?

I should have said:

therefore, '1122005' is 11-Feb-2005.

Thanks.
 
L

Lee

Evertjan. said:
Lee wrote on 15 mrt 2005 in comp.lang.javascript:


They are given by the OP.

Why improving on a reasonable request,
instead of showing the asked code?

It's not a reasonable request. Just because the OP believes
that every user will choose one of those three formats, why
in the world should you go along with his fantasy?
 
M

Morris

McKirahan said:
Zifud said:
McKirahan wrote:
[...]
"... e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'." doesn't include
'd-mm-yyy';
therefore, '1122005' is November 2, 2005.

Based on the suggested format, I make it 1 December 2005, or it
may be 11 February 2005.

Or does ddmmyyyy really mean mmddyyyy?

I should have said:

therefore, '1122005' is 11-Feb-2005.

Thanks.

'1122005' is either 1-Dec-2005 or 11-Feb-2005.

To remove any ambiguity, you should always use two digits for the day and
the month.
 
M

McKirahan

'1122005' is either 1-Dec-2005 or 11-Feb-2005.

To remove any ambiguity, you should always use two digits for the day and
the month.

Duh! Jumping in a little late aren't we...

You should have read the full thread first.
 
M

Matt Kruse

Lee said:
It's not a reasonable request. Just because the OP believes
that every user will choose one of those three formats, why
in the world should you go along with his fantasy?

It is just as unreasonable to assume that you know and understand every
situation that could exist. Enough so to state that the original request is
unreasonable.

A reasonable response would be to answer the question, then point out the
possible problems with such a situation. The OP can consider them if
necessary, or decide that they don't apply.
 
M

Matt Kruse

Martin said:
I am working on a HTML form in which a date must be entered of the
form 'dd-mm-yyyy'. Now I'm looking for a script that, when the user
switches to another form field, changes e.g. 'ddmmyyyy' or 'dd-m-yy'
to 'dd-mm-yyyy'.

Using my functions at: http://www.mattkruse.com/javascript/date/

function switchDate(obj) {
var d = parseDate(obj.value);
if (d==null) {
obj.value = "";
return;
}
obj.value = formatDate(d,"dd-MM-yyyy");
}

<input type="text" name="date" onChange="switchDate(this)">

// ------------------------------------------------------------------
// parseDate( date_string [, prefer_euro_format] )
//
// This function takes a date string and tries to match it to a
// number of possible date formats to get the value. It will try to
// match against the following international formats, in this order:
// y-M-d MMM d, y MMM d,y y-MMM-d d-MMM-y MMM d
// M/d/y M-d-y M.d.y MMM-d M/d M-d
// d/M/y d-M-y d.M.y d-MMM d/M d-M
// A second argument may be passed to instruct the method to search
// for formats like d/M/y (european format) before M/d/y (American).
// Returns a Date object or null if no patterns match.
// ------------------------------------------------------------------

Disclaimers:

(1) Be aware that users may enter dates in formats that you aren't
expecting, and may be surprised at the results they see.

(2) Always validate on the server-side if your code depends on a certain
format.
 
L

Lee

Matt Kruse said:
It is just as unreasonable to assume that you know and understand every
situation that could exist. Enough so to state that the original request is
unreasonable.

A reasonable response would be to answer the question, then point out the
possible problems with such a situation. The OP can consider them if
necessary, or decide that they don't apply.

"Here's your answer, but it won't work."

I'll leave that to others, thanks.
 
M

Matt Kruse

Lee said:
"Here's your answer, but it won't work."
I'll leave that to others, thanks.

The point is, you don't know it won't work. Because you don't know the
entire situation.

Furthermore, not every solution needs to be 100% fool-proof to provide
value.

If someone types 1/2/2005 into the OP's form and it's changed to 02-01-2005
when they really meant 01-02-2005, then the user will see that the date is
wrong and correct it.

Or, maybe the user wants this for an intranet in a single country where
everyone will type in dates in the same format. Or maybe a web site that
only serves a local community, where everyone will enter dates in the same
format.

You never know.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Tue, 15 Mar 2005 21:08:33, seen in Martin
Herrman said:
I am working on a HTML form in which a date must be entered of the form
'dd-mm-yyyy'. Now I'm looking for a script that, when the user switches to
another form field, changes e.g. 'ddmmyyyy' or 'dd-m-yy' to 'dd-mm-yyyy'.

I have searched the net using google, but only found validators that show
an error if an incorrect format is used. (and I can't write one on my own)

Does someone has such a script for me?

If you cannot trust the users to get the correct field format and
separation, can you trust the field order? And can you trust the
value??

You should be calling for an ISO 8601 format, with Y M D in that order;
it's probably also an EN and Dutch standard, and no-one uses yyyy-dd-mm.

One approach would be to consider what transformations on the input
cannot affect the date represented and bring the string nearer correct.

If the date is given as all digit, then to be unambiguous it must be
8-digit; 7 could be dmmyyyy or ddmyyyy, 6 could be dmyyyy or ddmmyy, 5
could be ddmyy or dmmyy. Actually, 4 could only be dmyy, but you might
consider that unreasonable - // ?

function FudgeDate(S) { var A, m = "-", O = '0'
if (/^(\d\d)(\d\d)(\d\d\d\d)$/.test(S))
with (RegExp) return $1 + m + $2 + m + $3
if (/^(\d)(\d)(\d\d)$/.test(S)) // ?
with (RegExp) return O + $1 + m + O + $2 + m + "20" + $3 // ?
A = S.split(/\D+/)
if (A.length!=3) return
if (+A[2]<100) A[2] = +A[2] + 2000
if (A[1].length<2) A[1] = O + A[1]
if (A[0].length<2) A[0] = O + A[0]
return A[0] + m + A[1] + m + A[2] }


There's something better than using test & $1 $2 $3 that way; but I
don't recall it at the moment.

You should also validate the date, to reject such as Feb 31st; to do
that, modify the above to return in all cases an array or object
containing the three numbers Y M D, and validate with a Date Object.

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 */ }


function ValidDate(y, m, d) { var D // m = 0..11 ; y m d integers, y!=0
with (D = new Date(y, m, d))
return getMonth()==m && getDate()==d ? D : NaN }


function ValidDate(y, m, d) { var D // m = 0..11 ; y m d integers, y!=0
with (D = new Date(y, m, d))
if (getMonth()==m && getDate()==d) return D }

respectively returning true/false, DateObject/NaN, DateObject/undefined.

(N.B. y==0 is harmless, but the result agrees with Rome rather than
Gregory)

See below.
 
L

Lee

Matt Kruse said:
The point is, you don't know it won't work. Because you don't know the
entire situation.

So just because I don't believe it will work, I should post an
answer because it just might, in the poster's unstated special
circumstances?

It seems to me to be more prudent and efficient to point out
serious problems before wasting time developing a solution.
 
M

Matt Kruse

Lee said:
It seems to me to be more prudent and efficient to point out
serious problems before wasting time developing a solution.

My response took about 1 minute to post, and it seems to address the
poster's request perfectly.
I even added some disclaimers.
And I wasn't an ass about it.
*shrug*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top