Re: html form: date field auto-adjust
Martin Herrman wrote:[color=blue]
> 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'.[/color]
Using my functions at: http://www.mattkruse.com/javascript/date/
function switchDate(obj) {
var d = parseDate(obj.v alue);
if (d==null) {
obj.value = "";
return;
}
obj.value = formatDate(d,"d d-MM-yyyy");
}
<input type="text" name="date" onChange="switc hDate(this)">
// ------------------------------------------------------------------
// parseDate( date_string [, prefer_euro_for mat] )
//
// 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.
--
Matt Kruse
Martin Herrman wrote:[color=blue]
> 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'.[/color]
Using my functions at: http://www.mattkruse.com/javascript/date/
function switchDate(obj) {
var d = parseDate(obj.v alue);
if (d==null) {
obj.value = "";
return;
}
obj.value = formatDate(d,"d d-MM-yyyy");
}
<input type="text" name="date" onChange="switc hDate(this)">
// ------------------------------------------------------------------
// parseDate( date_string [, prefer_euro_for mat] )
//
// 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.
--
Matt Kruse
Comment