473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

format date in textbox

Hi,
Here is what I'm trying to achieve:
The user enters in the textbox a date. It can be:
02.05.2006 or any other separator...
02052006
020506

any other formats should be forbidden.

When the user leaves the textbox then the date should be displayed as
02 May 2006.

How can I do that?
Thanks

Jun 16 '06 #1
9 14587
sa*************@googlemail.com said the following on 6/16/2006 12:20 PM:
Hi,
Here is what I'm trying to achieve:
The user enters in the textbox a date. It can be:
02.05.2006 or any other separator...
02052006
020506

any other formats should be forbidden.
"any other separator"? Hmmm. Can I use 0 or \ as the separator?

Before gaining success, you are going to have to limit your inputs some
more with regards to the separator.
When the user leaves the textbox then the date should be displayed as
02 May 2006.

How can I do that?


Use select lists.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 16 '06 #2
yes any separator. basically a if the string is 6, 8 or 10 characters
long it's valid:
05/06/2006 -> 10
05062006 -> 8
050606 -> 6

I don't want to use select lists, I want the user to enter the date as
fast as possible with no harsle.
I can still check later if the date is valid using .net functions (I do
asp.net)
All I want is the javascript to format the date as I mentionned earlier
when the textbox loses the focus.

cheers

Randy Webb a écrit :
sa*************@googlemail.com said the following on 6/16/2006 12:20 PM:
Hi,
Here is what I'm trying to achieve:
The user enters in the textbox a date. It can be:
02.05.2006 or any other separator...
02052006
020506

any other formats should be forbidden.


"any other separator"? Hmmm. Can I use 0 or \ as the separator?

Before gaining success, you are going to have to limit your inputs some
more with regards to the separator.
When the user leaves the textbox then the date should be displayed as
02 May 2006.

How can I do that?


Use select lists.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Jun 17 '06 #3
sa*************@googlemail.com writes:

Please don't top post.
yes any separator. basically a if the string is 6, 8 or 10 characters
long it's valid:
05/06/2006 -> 10
05062006 -> 8
050606 -> 6


But how about 1-005-07?

Is 050537 the same as 05.05.1937 or 05.05.2037?

If we assume digits cannot be separators, and the format must be
two digits, optional separators, two digits, optional separator,
two or four digits
and two digit-years are after 1970, then the following should work:

---
<style type="text/css">
.error {border:1px solid red;}
</style>
<input type="text" onchange="checkAndFormat(this);">
<input type="button" value="other">
<script type="text/javascript">
var monthName = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
function addErrorClass(elem) {
if (!elem.className.match(/\berror\b/)) {
elem.className += " error";
}
}
function removeErrorClass(elem) {
elem.className = elem.className.replace(/\berror\b(s*)/g,"$1");
}
function checkAndFormat(input) {
var value = input.value;
var match = /^\s*(\d\d)\D*(\d\d)\D*(\d\d(\d\d)?)\s*$/.exec(value);
if (match) {
var date = Number(match[1]);
var month = Number(match[2])-1;
var yearString = match[3];
var year = Number(match[3]);
if (yearString.length == 2) {
year += (year >= 70) ? 1900 : 2000;
}
// check validity of numbers
var actualDate = new Date();
actualDate.setFullYear(year, month, date);
if (actualDate.getDate() != date || actualDate.getMonth() != month) {
// illegal date, e.g., 35/02/06 ot 29/02/05
alert(["baddate",date,month,year,actualDate]);
addErrorClass(input);
} else {
input.value = (date < 10 ? "0" : "") + date + " " +
monthName[month] + " " + year;
removeErrorClass(input);
}
} else {
addErrorClass(input);
}
}
</script>
---
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 17 '06 #4
sa*************@googlemail.com wrote:
yes any separator. basically a if the string is 6, 8 or 10 characters
long it's valid:
05/06/2006 -> 10
05062006 -> 8
050606 -> 6

I don't want to use select lists, I want the user to enter the date as
fast as possible with no harsle.
I can still check later if the date is valid using .net functions (I do
asp.net)
All I want is the javascript to format the date as I mentionned earlier
when the textbox loses the focus.


Your problem is that the format you convert it to is not valid by your
criteria, so if the input goes out of focus again, an error will result.
Over to you...

<script type="text/javascript">

var monthNames = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];

function doDate( el, erID )
{
var s = el.value;
var d, m, y;
var erString = 'Not a nice date...';
var len = s.length;
if (10 == len){
d = s.substring(0,2);
m = s.substring(3,5);
y = s.substring(6,10);
} else if (8 == len) {
d = s.substring(0,2);
m = s.substring(2,4);
y = s.substring(4,8);
} else if (6 == len) {
d = s.substring(0,2);
m = s.substring(2,4);
y = '20' + s.substring(4,6);
}
if (checkDate(y,m,d)){
el.value = d + ' ' + monthNames[m-1] + ' ' + y;
erString = '';
}
if (document.getElementById){
document.getElementById(erID).innerHTML = erString;
}
if (erString){
if (el.focus) el.focus();
}
}

function checkDate(y, m, d)
{
m = '' + (m-1);
var checkDate = new Date(y,m,d);
return ( checkDate.getMonth() == m
&& checkDate.getFullYear() == y);
}

</script>

<input type="text" onblur="doDate(this, 'em_Date');">
<span id="em_Date"></span>
--
Rob
Jun 17 '06 #5
Thank you so much! Exactely what I was looking for!
RobG a écrit :
sa*************@googlemail.com wrote:
yes any separator. basically a if the string is 6, 8 or 10 characters
long it's valid:
05/06/2006 -> 10
05062006 -> 8
050606 -> 6

I don't want to use select lists, I want the user to enter the date as
fast as possible with no harsle.
I can still check later if the date is valid using .net functions (I do
asp.net)
All I want is the javascript to format the date as I mentionned earlier
when the textbox loses the focus.


Your problem is that the format you convert it to is not valid by your
criteria, so if the input goes out of focus again, an error will result.
Over to you...

<script type="text/javascript">

var monthNames = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];

function doDate( el, erID )
{
var s = el.value;
var d, m, y;
var erString = 'Not a nice date...';
var len = s.length;
if (10 == len){
d = s.substring(0,2);
m = s.substring(3,5);
y = s.substring(6,10);
} else if (8 == len) {
d = s.substring(0,2);
m = s.substring(2,4);
y = s.substring(4,8);
} else if (6 == len) {
d = s.substring(0,2);
m = s.substring(2,4);
y = '20' + s.substring(4,6);
}
if (checkDate(y,m,d)){
el.value = d + ' ' + monthNames[m-1] + ' ' + y;
erString = '';
}
if (document.getElementById){
document.getElementById(erID).innerHTML = erString;
}
if (erString){
if (el.focus) el.focus();
}
}

function checkDate(y, m, d)
{
m = '' + (m-1);
var checkDate = new Date(y,m,d);
return ( checkDate.getMonth() == m
&& checkDate.getFullYear() == y);
}

</script>

<input type="text" onblur="doDate(this, 'em_Date');">
<span id="em_Date"></span>


--
Rob


Jun 17 '06 #6
sa*************@googlemail.com wrote:
Thank you so much! Exactely what I was looking for!


Lasse's idea of using onchange is much better than my use of onblur.

[...]

--
Rob
Jun 18 '06 #7
JRS: In article <4493ed29$0$31258$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Sat, 17 Jun 2006 21:53:13 remote, seen in
news:comp.lang.javascript, RobG <rg***@iinet.net.au> posted :

function checkDate(y, m, d)
{
m = '' + (m-1);
var checkDate = new Date(y,m,d);
return ( checkDate.getMonth() == m
&& checkDate.getFullYear() == y);
}

Why convert (m-1) to a string when new Date(y, m, d) takes
numeric arguments and .getMonth() returns a Number?

Do you see an advantage, for valid A.D. years, in checking getFullYear
rather than getDate() ?

--
© 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.
Jun 18 '06 #8
JRS: In article <Yq******************************@comcast.com>, dated
Fri, 16 Jun 2006 15:42:41 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.com> posted :
sa*************@googlemail.com said the following on 6/16/2006 12:20 PM:
Hi,
Here is what I'm trying to achieve:
The user enters in the textbox a date. It can be:
02.05.2006 or any other separator...
02052006
020506

any other formats should be forbidden.


"any other separator"? Hmmm. Can I use 0 or \ as the separator?


Digit zero cannot separate digit fields (which is why in my program
NOWMINUS I use i0 to mean "no separator", i already meaning "space
separator".

I see no real difficulty in using \, though it might break some code.

If D M & Y are separated numeric fields, the OP can allow single-digit D
& M; single-digit Y is unreasonable.

--
© 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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jun 18 '06 #9
Dr John Stockton said the following on 6/18/2006 9:23 AM:
JRS: In article <Yq******************************@comcast.com>, dated
Fri, 16 Jun 2006 15:42:41 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.com> posted :
sa*************@googlemail.com said the following on 6/16/2006 12:20 PM:
Hi,
Here is what I'm trying to achieve:
The user enters in the textbox a date. It can be:
02.05.2006 or any other separator...
02052006
020506

any other formats should be forbidden.

"any other separator"? Hmmm. Can I use 0 or \ as the separator?


Digit zero cannot separate digit fields (which is why in my program
NOWMINUS I use i0 to mean "no separator", i already meaning "space
separator".

I see no real difficulty in using \, though it might break some code.


It was a rhetorical question John. Nothing more, nothing less and it
made my point.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 18 '06 #10

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

Similar topics

5
by: Daveo | last post by:
Hi there, I'm having a problem with my database changing the date that I enter in the format dd/mm/yyyy into American format. Bizarrely, when enter the date in a textbox it changes round, but if...
4
by: Jan Nielsen | last post by:
Hi I have a dataform that shows a date from a MSDE 2000 in a textbox. It uses the format: dd-mm-yyyy hh:mm:ss It would it to use the format dd-mm-yyyy But I can't find a Format property for the...
3
by: Slonocode | last post by:
I have some textboxes bound to an access db. I wanted to format the textboxes that displayed currency and date info so I did the following: Dim WithEvents oBidAmt As Binding oBidAmt = New...
1
by: Rich | last post by:
Hello, I have some datefields in a dataset (ds1). I bind some textbox controls on a windows form to these date fields in ds1, but I only want to see 01/01/2004 instead of 1/1/2004 8:00:00 AM. ...
16
by: Al Reid | last post by:
First, I'm using vb2005. I have a string that is read from a barcode reader into a TextBox. The string is 6 characters long and represents a date (mmddyy). I want to display it to the user in a...
2
by: Derek Vincent | last post by:
What must I do to overcome a problem with my dates becoming formatted as "2/22/2525 12:00:00 AM" in the datagrid? I want to handle all dates as short string of format "2/22/2525." Otherwise when I...
20
by: andreas | last post by:
When I copy a vb.net project using date formats from one PC with a windows date format f.e. dd/mm/yyyy to another PC having a format yy/mm/dd then I get errors. How can I change for a while in the...
7
by: Richiep | last post by:
I am trying to get a UK format date of dd/mm/yyyy. Why does the following subroutine not return a valid date in a web form? The date returned is #12:00:00 AM# but the date I entered into the...
1
by: JFKJr | last post by:
Hello everyone, this one might be simple but driving me crazy! Your help will be greatly appreciated. Basically, I created a textbox bound to "Date" field in an Access VBA form. But when the...
14
by: | last post by:
Hi, I program in asp.net. I have a date in TextBox in format "dd/MM/yyyy". I would like to validate if the date is realy correct. I used RegularExpressionValidator with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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...

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.