#136534 - 2005-03-28 04:03 PM
IsNumeric RFC..
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Playing around with Howard's code which seems to be about the best I could find out there.. Code:
CLS BREAK ON ;http://www.kixtart.org/ubbthreads/showthreaded.php?Cat=&Number=109066 ;http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Board=UBB1&Number=126912 $text = "8", "a", "-12" ;$Pattern='^\d{1,3}$' ;This would match any 1,2, or 3 digit number. ;After check to verify that the the input passes this test ; do a simple check for greater then 0 and less then 266. for each $item in $text if RegExpTest("^\d{1,3}$", $item, 1) ? $item + "-Found match" else ? $item + "-No Match found" endif next get $ Function RegExpTest($Pattern, $String, $IgnoreCase) Dim $regEx, $Match, $Matches ; Create variable. $regEx = createobject("VBscript.RegExp") ; Create a regular expression. $regEx.Pattern = $Pattern ; Set pattern. $regEx.IgnoreCase = val($IgnoreCase) ; Set case insensitivity. $RegExpTest = $regEx.Test($String) ; Execute test search. EndFunction
The problem is that it does not seem to handle negative values very well.
Thanks,
Kent
|
Top
|
|
|
|
#136535 - 2005-03-28 04:15 PM
Re: IsNumeric RFC..
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
We can change that behavior by permitting an optional minus sign as the starting character. If the pattern to match was changed to lets say...
"^-?\d{1,3}$"
I do not believe that the hyphen is a metacharcter so this should work. If it does not then add a backslash before the hyphen to escape it.
("^\-?\d{1,3}$"
Quote:
Quantifier Description {num} Matches the preceding element num times. {min, max} Matches the preceding element at least min times, but not more than max times. ? Matches any preceding element 0 or 1 times. * Matches the preceding element 0 or more times. + Matches the preceding element 1 or more times.
|
Top
|
|
|
|
#136536 - 2005-03-28 04:46 PM
Re: IsNumeric RFC..
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
A more generic pattern would be "^-?\d+$" this should match any string that is one or more numerics optionally preceded by a minus sign. The example above that uses {1,3} only match a numeric string that is a minimum of 1 to a maximum of 3 numeric characters in length.
|
Top
|
|
|
|
#136537 - 2005-03-28 05:05 PM
Re: IsNumeric RFC..
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
and "^-?[,\d{1,3}]+$" would match positive or negative numerics that contained commas.
|
Top
|
|
|
|
#136538 - 2005-03-28 09:19 PM
Re: IsNumeric RFC..
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Thanks Howard! That works a treat..
Did some digging through some old code and found: Code:
;Function ISNUMERIC() ;Author Kent Dyer (leptonator@hotmail.com) ;Action Check for a numeric value ;Syntax ISNUMERIC("value") ;Version 1.0 ;Date 09 - March - 2004 ;Date Revised 09 - March - 2004 ;Parameters value ; Value to test for ;Remarks This is a simple check for a numeric value. ;Returns Check for the value being a numeric ;Dependencies None ;KiXtart Ver 4.02 ;Example(s) ?ISNUMERIC('123342334') ; ?ISNUMERIC('test') ; ?ISNUMERIC('-123342334') ?ISNUMERIC('123342334') ?ISNUMERIC('test') get $ FUNCTION ISNUMERIC($val) DIM $i,$res[1],$result IF CINT($val) $res='Integer' ELSE $res='nonInteger' ENDIF $ISNUMERIC=$res ENDFUNCTION
Kent
|
Top
|
|
|
|
#136539 - 2005-03-28 10:33 PM
Re: IsNumeric RFC..
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Kent, I think that function does not provide the level of checking you may need. try:
Code:
? "1test = " + ISNUMERIC('1test')
My result: Quote:
1test = Integer
Edited by Howard Bullock (2005-03-28 10:34 PM)
|
Top
|
|
|
|
#136540 - 2005-03-28 11:19 PM
Re: IsNumeric RFC..
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
I was thing about this.. If you take a var and raise it to a zero power, you still get 1. Problem with that though is if you think about it from a Algebraic term, you will still get a one.
So, agreed - your method works much better.
Kent
|
Top
|
|
|
|
#136542 - 2005-03-29 06:28 AM
Re: IsNumeric RFC..
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Then.. I guess it would be interesting to see what the code is behind the VB implementation..
Kent
|
Top
|
|
|
|
#136547 - 2005-03-29 04:19 PM
Re: IsNumeric RFC..
|
Shawn
Administrator
Registered: 1999-08-13
Posts: 8611
|
Jooel, here's another twist on your UDF ... actually returns Booleans ...
Code:
function isNumeric($var) $IsNumeric = iif($var=0.0+$var,NOT 0,NOT 1) endfunction
|
Top
|
|
|
|
#136549 - 2005-03-29 05:37 PM
Re: IsNumeric RFC..
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Jooel got it!
Shouldn't you create a UDF for this?
Kent
|
Top
|
|
|
|
#136551 - 2005-03-29 05:49 PM
Re: IsNumeric RFC..
|
maciep
Korg Regular
Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
|
Anything wrong with
Code:
function isNumeric($var) $IsNumeric = ($var=0.0+$var) endfunction
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 507 anonymous users online.
|
|
|