Onchange correction on date

S

Simon

Hello,
I'm in need of a script that will adjust a date in a textfield.
For example: I type: 31012005 and leave this textfield. On leaving,
javascript should correct this to 31-01-2005.
Can anybody help me with this?

Thanks,
Simon
 
A

alu

Simon said:
Hello,
I'm in need of a script that will adjust a date in a textfield.
For example: I type: 31012005 and leave this textfield. On leaving,
javascript should correct this to 31-01-2005.
Can anybody help me with this?

Thanks,
Simon


You might want to look up the String.length property, and the methods
String.substr, String.sub, and String.concat
See any javascript site, e.g.,
http://www.javascriptkit.com/jsref/string.shtml
-alu
 
M

McKirahan

Simon said:
Hello,
I'm in need of a script that will adjust a date in a textfield.
For example: I type: 31012005 and leave this textfield. On leaving,
javascript should correct this to 31-01-2005.
Can anybody help me with this?

Thanks,
Simon


Will this help? Watch for word-wrap.

<html>
<head>
<title>ddmmyyyy.htm</title>
<script type="text/javascript">
function formatDate(that,goto) {
if (that.value.length != 8) return;
var what = that.value.substr(0,2) + "-";
what += that.value.substr(2,2) + "-";
what += that.value.substr(4);
that.value = what;
document.getElementById(goto).focus();
}
</script>
</head>
<body>
<form>
<input type="text" name="date1" id="date1"
size="10" maxlength="10" value="" onkeyup="formatDate(this,'text1')">
<input type="text" name="text1" id="text1"
size="10" maxlength="10" value="">
</form>
</body>
</html>


FYI -- the format "yyyy-mm-dd" is preferable to "dd-mm-yyyy".
 
M

Matt Kruse

Simon said:
I'm in need of a script that will adjust a date in a textfield.
For example: I type: 31012005 and leave this textfield. On leaving,
javascript should correct this to 31-01-2005.

Aside from the fact that you can't know for sure what format the user typed
in his date (could be ddMMyyyy or MMddyyyy, etc) you can use my date
functions to easily check the format and validity, and do the transform:
http://www.JavascriptToolbox.com/date/
 
M

McKirahan

Simon said:
Thanks, "McKirihan", I used your script, it works well.

[snip]

Here's a version with the "John Stockton" approach:

<html>
<head>
<title>ddmmyyyy.htm</title>
<script type="text/javascript">
function formatDate(that,goto) {
if (that.value.length != 8) return;
that.value = that.value.replace(/^(\d\d)(\d\d)(\d\d\d\d)$/, "$1-$2-$3")
document.getElementById(goto).focus();
}
</script>
</head>
<body>
<form>
<input type="text" name="date1" id="date1" value=""
onkeyup="formatDate(this,'text1')">
<input type="text" name="text1" id="text1" value="">
</form>
</body>
</html>
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 23 Jun
2005 08:38:18, seen in McKirahan
function formatDate(that,goto) {
if (that.value.length != 8) return;
that.value = that.value.replace(/^(\d\d)(\d\d)(\d\d\d\d)$/, "$1-$2-$3")
document.getElementById(goto).focus();
}

Method getElementById is not always available.

If the replacement did not occur, focus should not be changed.

By checking that the character entered was a digit, and removing it
otherwise, one could be sure that at 8 characters the replacement would
occur. Use that.value = that.value.replace(/\D+/g, "") to also
deal with pasting in by ^V .
 
A

alu

Dr John Stockton said:
ISTM more easily done by RegExp, along the lines of

X = X.replace(/^(\d\d)(\d\d)(\d\d\d\d)$/, "$1-$2-$3")

What a great group; I try to contribute a bit occasionally, and learn a lot
in the process.
Thanks John; I'd forgotten about the all-powerful RegExp - time to do some
reading.
-alu
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top