Hello Developers,

Ever tried response.write IsDate("2 A") ?
Well is gives True
It is also becomes True by using data between 0 A tot 23 A.
Any ideas on solving that problem.

I used IsDate in IIS .asp / VB

Kind regards,

Tony J

Re: IsDate("2 A") = True ? by ekkehard

ekkehard
Fri Dec 07 04:55:11 PST 2007

Tony J schrieb:
> Hello Developers,
>
> Ever tried response.write IsDate("2 A") ?
> Well is gives True
> It is also becomes True by using data between 0 A tot 23 A.
> Any ideas on solving that problem.
>
> I used IsDate in IIS .asp / VB
>
> Kind regards,
>
> Tony J
>
>
IsDate() - and CDate() - take a 'when in doubt, accept/convert it' approach.
So IsDate() == True should be considered a necessary but not sufficient
condition for 'dateness'. When processing user input, a syntax check (RegExp)
and/or comparing the user's string with the result of CDate( <user's string> )
is advisable.

J R Stockton's website - e.g.

http://www.merlyn.demon.co.uk/vb-dates.htm

is a good resource for ideas/code/caveats regarding dates.

Re: IsDate("2 A") = True ? by Bob

Bob
Fri Dec 07 04:56:58 PST 2007

Tony J wrote:
> Hello Developers,
>
> Ever tried response.write IsDate("2 A") ?
> Well is gives True
> It is also becomes True by using data between 0 A tot 23 A.
> Any ideas on solving that problem.
>
> I used IsDate in IIS .asp / VB
>
> Kind regards,
>
> Tony J
I know you don't want to hear this, but it's working the way it's supposed
to. IsDate returns true if the value can be converted to a date. You think
"2 A" cannot be converted to a date? Try this:

Response.Write CDate("2 A")

Google for date validation in vbscript for help with this:
http://www.google.com/search?sourceid=ie7&rls=com.microsoft:en-US&ie=utf8&oe=utf8&q=vbscript+date+validation

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"



Re: IsDate("2 A") = True ? by Alexander

Alexander
Fri Dec 07 05:03:56 PST 2007

Tony J schrieb:
> Hello Developers,
>
> Ever tried response.write IsDate("2 A") ?
> Well is gives True
> It is also becomes True by using data between 0 A tot 23 A.
> Any ideas on solving that problem.
>
> I used IsDate in IIS .asp / VB


It works with 'a' and 'p' and also with
'am' and 'pm' because this stands for
'ante meridiem' and 'post meridiem' in the US
and the UK.

Nonthelesse it not very precise because
'22:22 am' looks kind of invalid to me, but
returns true for IsDate.

MfG,
Alex

Re: IsDate("2 A") = True ? by Tony

Tony
Fri Dec 07 05:59:39 PST 2007

Thank you Alexander for a quick response,

I can live with your answer.
Never tried before with P.

It seems a solutions needs to be with IsDate after a syntax check (RegExp)
like Ekkehard Horner tells us.

Very interesting > http://www.merlyn.demon.co.uk/vb-date1.htm

My solution:

Function IsDateRegExp(dDate)
Dim bCheck
'--- Set object
Set RegularExpressionObject = New regexp
'--- Patern and options
RegularExpressionObject.Pattern = "^(0[1-9]|[12][0-9]|3[01])[-
/.](0[1-9]|1[012])[- /.](19|20)\d\d$"
'--- dd/mm/yyyy (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[-
/.](19|20)\d\d
'--- mm/dd/yyyy (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[-
/.](19|20)\d\d
'--- yyyy/mm/dd (19|20)\d\d[- /.](0[1-9]|1[012])[-
/.](0[1-9]|[12][0-9]|3[01])
RegularExpressionObject.Global = False
'--- Test date
IF RegularExpressionObject.Test(dDate) and IsDate(dDate) THEN '---
IsDate is needed for check existence of the date
IsDateRegExp = True
ELSE
IsDateRegExp = False
END IF
'--- Kill Object
Set RegularExpressionObject = Nothing
END FUNCTION

Kind regards,

Tony J

"Alexander Mueller" <millerax@hotmail.com> schreef in bericht
news:475944bc$0$13113$9b4e6d93@newsspool2.arcor-online.net...
> Tony J schrieb:
>> Hello Developers,
>>
>> Ever tried response.write IsDate("2 A") ?
>> Well is gives True
>> It is also becomes True by using data between 0 A tot 23 A.
>> Any ideas on solving that problem.
>>
>> I used IsDate in IIS .asp / VB
>
>
> It works with 'a' and 'p' and also with
> 'am' and 'pm' because this stands for
> 'ante meridiem' and 'post meridiem' in the US
> and the UK.
>
> Nonthelesse it not very precise because
> '22:22 am' looks kind of invalid to me, but
> returns true for IsDate.
>
> MfG,
> Alex



Re: IsDate("2 A") = True ? by ekkehard

ekkehard
Fri Dec 07 07:29:00 PST 2007

Tony J schrieb:
> Thank you Alexander for a quick response,
>
> I can live with your answer.
> Never tried before with P.
>
> It seems a solutions needs to be with IsDate after a syntax check (RegExp)
> like Ekkehard Horner tells us.
>
> Very interesting > http://www.merlyn.demon.co.uk/vb-date1.htm
>
> My solution:
>
> Function IsDateRegExp(dDate)
> Dim bCheck
> '--- Set object
> Set RegularExpressionObject = New regexp
> '--- Patern and options
> RegularExpressionObject.Pattern = "^(0[1-9]|[12][0-9]|3[01])[-
> /.](0[1-9]|1[012])[- /.](19|20)\d\d$"
> '--- dd/mm/yyyy (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[-
> /.](19|20)\d\d
> '--- mm/dd/yyyy (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[-
> /.](19|20)\d\d
> '--- yyyy/mm/dd (19|20)\d\d[- /.](0[1-9]|1[012])[-
> /.](0[1-9]|[12][0-9]|3[01])
> RegularExpressionObject.Global = False
> '--- Test date
> IF RegularExpressionObject.Test(dDate) and IsDate(dDate) THEN '---
> IsDate is needed for check existence of the date
> IsDateRegExp = True
> ELSE
> IsDateRegExp = False
> END IF
> '--- Kill Object
> Set RegularExpressionObject = Nothing
> END FUNCTION
>
> Kind regards,
>
> Tony J
[...]
This certainly is a good solution that shows, the author cares for his code.
That's why I'd like to show/give reasons for a slightly different version:

Function IsDateRegExp( sDate )
With New RegExp
.Pattern = "^(0[1-9]|[12][0-9]|3[01])[-/.](0[1-9]|1[012])[- /.](19|20)\d\d$"
IsDateRegExp = .Test( sDate ) and IsDate( sDate )
End With
End Function

(1) sDate (vs. dDate) shows explicitly/correctly that the function expects
a string

(2) As VBScript doesn't warn you about variables not used (bCheck) and
"Option Explicit" isn't mandatory, I prefer short variables names
(e.g. oRE) or even avoiding them. (Second opinion: With .. End With
may be inefficient.)

(3) Booleans should be used as a first class datatype. Extra Assigning
and/or comparing to True/False has no benefits, distracts from what the
code really does, and increases the risk of bugs. If you accidentially
write

IsDateRegExp = Fa1se

and your users are carefull, it may be weeks until the script breaks
with "no variable Fa1se". (Second opinion: Some people prefer using
True/False often to improve clarity.)

(4) While it may be reasonable to Set a database connection to Nothing in
the middle of a very long script/function/sub, it's futile to do so
at the end. If VBScript doesn't do it automatically when the variable
goes out of scope, you found a bug; if you wrote

Set RegularExpressionObject = Nohting

you created a bug for Nothing.

I'm not claiming that my code/reasons are better or to be prefered under
all circumstances, but I hope they can be seen as food for thought.




Re: IsDate("2 A") = True ? by Dr

Dr
Sat Dec 08 11:03:50 PST 2007

In microsoft.public.scripting.vbscript message <#a4UxlNOIHA.1212@TK2MSFT
NGP05.phx.gbl>, Fri, 7 Dec 2007 14:59:39, Tony J <tony@creations.be>
posted:
>
>My solution:
>
>Function IsDateRegExp(dDate)
> Dim bCheck
> '--- Set object
> Set RegularExpressionObject = New regexp
> '--- Patern and options
> RegularExpressionObject.Pattern = "^(0[1-9]|[12][0-9]|3[01])[-
>/.](0[1-9]|1[012])[- /.](19|20)\d\d$"
> '--- dd/mm/yyyy (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[-
>/.](19|20)\d\d
> '--- mm/dd/yyyy (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[-
>/.](19|20)\d\d
> '--- yyyy/mm/dd (19|20)\d\d[- /.](0[1-9]|1[012])[-
>/.](0[1-9]|[12][0-9]|3[01])
> RegularExpressionObject.Global = False
> '--- Test date
> IF RegularExpressionObject.Test(dDate) and IsDate(dDate) THEN '---
>IsDate is needed for check existence of the date
> IsDateRegExp = True
> ELSE
> IsDateRegExp = False
> END IF
> '--- Kill Object
> Set RegularExpressionObject = Nothing
>END FUNCTION

Near end :
if X then
A = true
else
A = false
is always unnecessary - use just A = X.
Study Booleans until you understand why.

As you know, isDate is well-nigh useless, except for those who want
their code to run without confessed error but do not care whether the
answer is good.

IMHO, one rarely needs to check the validity of a date FULL STOP (period
in USA). One generally needs to read its value and know whether that is
good.

Numeric-type checking with a RegExp is generally lengthy, complex, and
error-prone. Your limit to 19xx & 20xx can easily be done with a
numeric check (and will lead to eventual system failure). And I think
you don't check for normal month-length, let alone the effects of Leap
Years.

The best move, therefore, is to check the input by RegExp for having an
acceptable pattern of digits and separators, including no more than two
Date digits, then to use DateSerial to get a CDate, then to check the
Month part for matching the input.

See REcheckDate in <URL:http://www.merlyn.demon.co.uk/vb-date1.htm#SaR>,
noting that it can be simplified for any specific date field order and
exact pattern. It gives a full Gregorian check, including all month-
length. Note that, as is, it is liberal about field separation, not
even requiring that the separators be the same. The latter could easily
be achieved if VBS RegExps understand back references.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
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.

Re: IsDate("2 A") = True ? by ekkehard

ekkehard
Sun Dec 09 00:43:13 PST 2007

Dr J R Stockton schrieb:
> In microsoft.public.scripting.vbscript message <#a4UxlNOIHA.1212@TK2MSFT
> NGP05.phx.gbl>, Fri, 7 Dec 2007 14:59:39, Tony J <tony@creations.be>
> posted:
>> My solution:
[...]
>>
> See REcheckDate in <URL:http://www.merlyn.demon.co.uk/vb-date1.htm#SaR>,
> noting that it can be simplified for any specific date field order and
> exact pattern. It gives a full Gregorian check, including all month-
> length. Note that, as is, it is liberal about field separation, not
> even requiring that the separators be the same. The latter could easily
> be achieved if VBS RegExps understand back references.

If you are interested in the RegExp check:

Dim aPats : aPats = Array( _
"^\s*([1-8]\d\d\d)\D*(\d\d)\D*(\d\d)\s*$" , "\D* zero or more non digits" _
, "^\s*([1-8]\d\d\d)\D?(\d\d)\D?(\d\d)\s*$" , "\D? one or more non digits" _
, "^\s*([1-8]\d\d\d)(\D?)(\d\d)\2(\d\d)\s*$" , "\2 back reference to second ()" _
)

Dim aTests : aTests = Array( _
"2007xx12xx31", "2007/12.31", "2007.12.31" _
)

Dim oRE : Set oRE = New RegExp

Dim nIdx, sDate
For nIdx = 0 To UBound( aPats ) Step 2
WScript.Echo "------", aPats( nIdx ), aPats( nIdx + 1 )
oRE.Pattern = aPats( nIdx )
For Each sDate In aTests
WScript.Echo sDate, vbTab, CStr( oRE.Test( sDate ) )
Next
Next

output:

=== checkDateJRS: check date (J R Stockton) ===================================
------ ^\s*([1-8]\d\d\d)\D*(\d\d)\D*(\d\d)\s*$ \D* zero or more non digits
2007xx12xx31 Wahr
2007/12.31 Wahr
2007.12.31 Wahr
------ ^\s*([1-8]\d\d\d)\D?(\d\d)\D?(\d\d)\s*$ \D? one or more non digits
2007xx12xx31 Falsch
2007/12.31 Wahr
2007.12.31 Wahr
------ ^\s*([1-8]\d\d\d)(\D?)(\d\d)\2(\d\d)\s*$ \2 back reference to second ()
2007xx12xx31 Falsch
2007/12.31 Falsch
2007.12.31 Wahr
=== checkDateJRS: 0 done (00:00:00) ===========================================


Re: IsDate("2 A") = True ? by Dr

Dr
Sun Dec 09 11:39:20 PST 2007

In microsoft.public.scripting.vbscript message <475baaa1$0$16665$9b4e6d9
3@newsspool3.arcor-online.net>, Sun, 9 Dec 2007 09:43:13,
ekkehard.horner <ekkehard.horner@arcor.de> posted:
>> Note that, as is, it is liberal about field separation, not
>> even requiring that the separators be the same. The latter could easily
>> be achieved if VBS RegExps understand back references.
>
>If you are interested in the RegExp check:
>
> Dim aPats : aPats = Array( _
> "^\s*([1-8]\d\d\d)\D*(\d\d)\D*(\d\d)\s*$" , "\D* zero or more non digits" _
> , "^\s*([1-8]\d\d\d)\D?(\d\d)\D?(\d\d)\s*$" , "\D? one or more non digits" _
> , "^\s*([1-8]\d\d\d)(\D?)(\d\d)\2(\d\d)\s*$" , "\2 back reference to second ()" _
> )

Ah yes, I now see the mistake I made in trying to test back-referencing
- wrong digit.
But, in your comment, \D? surely means 0 or 1 non-digits?

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
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.

Re: IsDate("2 A") = True ? by ekkehard

ekkehard
Sun Dec 09 22:47:19 PST 2007

Dr J R Stockton schrieb:
> In microsoft.public.scripting.vbscript message <475baaa1$0$16665$9b4e6d9
> 3@newsspool3.arcor-online.net>, Sun, 9 Dec 2007 09:43:13,
> ekkehard.horner <ekkehard.horner@arcor.de> posted:
>>> Note that, as is, it is liberal about field separation, not
>>> even requiring that the separators be the same. The latter could easily
>>> be achieved if VBS RegExps understand back references.
>> If you are interested in the RegExp check:
>>
>> Dim aPats : aPats = Array( _
>> "^\s*([1-8]\d\d\d)\D*(\d\d)\D*(\d\d)\s*$" , "\D* zero or more non digits" _
>> , "^\s*([1-8]\d\d\d)\D?(\d\d)\D?(\d\d)\s*$" , "\D? one or more non digits" _
>> , "^\s*([1-8]\d\d\d)(\D?)(\d\d)\2(\d\d)\s*$" , "\2 back reference to second ()" _
>> )
>
> Ah yes, I now see the mistake I made in trying to test back-referencing
> - wrong digit.
> But, in your comment, \D? surely means 0 or 1 non-digits?
>
Yes - caused by copy & paste & edit with brain switched off.

Re: IsDate("2 A") = True ? by Al

Al
Mon Dec 17 22:04:21 CST 2007


"Dr J R Stockton" <jrs@merlyn.demon.co.uk> wrote in message
news:BdGOiFYWquWHFw3e@invalid.uk.co.demon.merlyn.invalid...
> In microsoft.public.scripting.vbscript message <#a4UxlNOIHA.1212@TK2MSFT
> NGP05.phx.gbl>, Fri, 7 Dec 2007 14:59:39, Tony J <tony@creations.be>
> posted:
>>
>>My solution:
>>
>>Function IsDateRegExp(dDate)
>> Dim bCheck
>> '--- Set object
>> Set RegularExpressionObject = New regexp
>> '--- Patern and options
>> RegularExpressionObject.Pattern = "^(0[1-9]|[12][0-9]|3[01])[-
>>/.](0[1-9]|1[012])[- /.](19|20)\d\d$"
>> '--- dd/mm/yyyy (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[-
>>/.](19|20)\d\d
>> '--- mm/dd/yyyy (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[-
>>/.](19|20)\d\d
>> '--- yyyy/mm/dd (19|20)\d\d[- /.](0[1-9]|1[012])[-
>>/.](0[1-9]|[12][0-9]|3[01])
>> RegularExpressionObject.Global = False
>> '--- Test date
>> IF RegularExpressionObject.Test(dDate) and IsDate(dDate) THEN '---
>>IsDate is needed for check existence of the date
>> IsDateRegExp = True
>> ELSE
>> IsDateRegExp = False
>> END IF
>> '--- Kill Object
>> Set RegularExpressionObject = Nothing
>>END FUNCTION
>
> Near end :
> if X then
> A = true
> else
> A = false
> is always unnecessary - use just A = X.

This is only really equivalent when X contains an actual boolean value (i.e.
true or false), which is, of course, true in the example of interest. if X
will consider a non-zero numeric value of X as true, so the above
if-then-else code would result in A being set to true if X were non-zero or
to false if X had a zero value. Using A = X as you suggest above would
result in A being assigned the numeric value of X. Of course, if A is then
used in a simple boolean expression, no problem. But watch out when things
get a little less straightforward.

InStr is a good example of a function that is sometimes assumed to return a
boolean result of true if the substring is found or false if it is not. But
if one assumes that the result of InStr is *exactly* the same as if it had
been coded to mean IsInStr...

string1 = "abcdefg"
hasB = inStr(string1,"b")
hasD = inStr(string1,"d")
if hasB then
wscript.echo "contains B"
else
wscript.echo "has no B"
end if
if hasD then
wscript.echo "contains D"
else
wscript.echo "has no D"
end if
if hasB and hasD then
wscript.echo "contains B & D"
else
wscript.echo "does not contain B & D"
end if

But I agree that if the expression being tested can only be boolean, the
direct assignment makes far more sense. I seem to recall having a somewhat
lengthy discussion of this a while back and was surprised at how many people
seemed afraid to do it this way...

/Al

> Study Booleans until you understand why.
>
> As you know, isDate is well-nigh useless, except for those who want
> their code to run without confessed error but do not care whether the
> answer is good.
>
> IMHO, one rarely needs to check the validity of a date FULL STOP (period
> in USA). One generally needs to read its value and know whether that is
> good.
>
> Numeric-type checking with a RegExp is generally lengthy, complex, and
> error-prone. Your limit to 19xx & 20xx can easily be done with a
> numeric check (and will lead to eventual system failure). And I think
> you don't check for normal month-length, let alone the effects of Leap
> Years.
>
> The best move, therefore, is to check the input by RegExp for having an
> acceptable pattern of digits and separators, including no more than two
> Date digits, then to use DateSerial to get a CDate, then to check the
> Month part for matching the input.
>
> See REcheckDate in <URL:http://www.merlyn.demon.co.uk/vb-date1.htm#SaR>,
> noting that it can be simplified for any specific date field order and
> exact pattern. It gives a full Gregorian check, including all month-
> length. Note that, as is, it is liberal about field separation, not
> even requiring that the separators be the same. The latter could easily
> be achieved if VBS RegExps understand back references.
>
> --
> (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE
> 6.
> 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.



Re: IsDate("2 A") = True ? by Dr

Dr
Tue Dec 18 14:13:14 CST 2007

In microsoft.public.scripting.vbscript message <O98zSsSQIHA.4740@TK2MSFT
NGP02.phx.gbl>, Mon, 17 Dec 2007 21:04:21, Al Dunbar
<AlanDrub@hotmail.com.nospaam> posted:

>> Near end :
>> if X then
>> A = true
>> else
>> A = false
>> is always unnecessary - use just A = X.
>
>This is only really equivalent when X contains an actual boolean value (i.e.
>true or false), which is, of course, true in the example of interest. if X
>will consider a non-zero numeric value of X as true, so the above
>if-then-else code would result in A being set to true if X were non-zero or
>to false if X had a zero value. Using A = X as you suggest above would
>result in A being assigned the numeric value of X. Of course, if A is then
>used in a simple boolean expression, no problem. But watch out when things
>get a little less straightforward.

In the case you describe one can write A = !! X in JScript to get
the desired effect - that's probably astonishing when first seen but
easily recognised on all subsequent occasions. JScript has ~ for unary
numeric bitwise complement. VBscript, I think, has just the one unary
not operator, alas; one cannot use not not likewise to get Boolean.

In VBScript, given that one should know the type of X, I'd suggest A =
(X=0) or A = (X<>0), depending.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3? Turnpike 6.05
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
<URL:http://www.borland.com/newsgroups/guide.html> news:borland.* Guidelines