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 9 14530 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/
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/ 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.' 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
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 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
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.
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.
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| |