Page 1 of 1 1
Topic Options
#207461 - 2013-07-18 02:42 PM Test characters in a string
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Hi,

I need to make sure that a string variable contains numbers and no other characters. Is there a quick easy way to test for this?

Thanks


Edited by kelp7 (2013-07-18 02:49 PM)

Top
#207462 - 2013-07-18 03:09 PM Re: Test characters in a string [Re: kelp7]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Simple way:
 Code:
$Tmp = Val($NumberString)
If $Tmp <> $NumberString
  'ERROR - ' $NumberString ' contains non-numeric chars!' ?
EndIf
It's quick and dirty, but won't handle numbers containing commas, which might be legal. A better way would be to enumerate the string, comparing each char against a list of allowed chars - basic data validation:
 Code:
$Allowed = '0123456789.,'
For $P = 1 to Len($NumString)
  $C = SubStr($NumString, $P, 1
  If Not InStr($Allowed, $C)
    $C ' is an invalid char - aborting!' ?
    Exit 1
  EndIf
Next
This same logic can be used to validate any type of data.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#207463 - 2013-07-18 04:02 PM Re: Test characters in a string [Re: Glenn Barnas]
kelp7 Offline
Starting to like KiXtart

Registered: 2002-08-12
Posts: 124
Loc: UK
Thanks Glenn! Both suggestions are great.... the 2nd is particularly excellent as I may need to allow just some small number of other non-numeric characters \:\) Brilliant though! Thank you!
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.061 seconds in which 0.032 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org