Page 1 of 1 1
Topic Options
#203296 - 2011-10-20 01:11 PM Validate a variable contains numbers
GeorgeLittle Offline
Fresh Scripter

Registered: 2011-02-08
Posts: 47
Loc: UK
Hello

I have a variable lets call it $uid it should contain a 5 digit number . What I would like to do is check it's

Is only numeric

Could anyone be so kind to point me in the directioon

Top
#203297 - 2011-10-20 01:27 PM Re: Validate a variable contains numbers [Re: GeorgeLittle]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
This works for me.

 Code:
Break on

$uid = "12345"

If IsNumeric($uid) = 1
	? "UID is numeric."
Else
	? "UID is NOT numeric."
EndIf

;Function       IsNumeric()
;
;Author:	apronk
;
;Action:
;		Determines whether an expression is a valid numeric type.
;
;Syntax:
;		IsNumeric($strInput)
;
;Version:
;		1.0 December 11th 2009.
;		Initial post.
;
;Parameters:
;		$strInput - Required.
;			The input string to evaluate.
;
;Returns:
;		1 if the input is Numeric
;		0 if the input is NOT Numeric.
;
;Dependencies
;		No known dependencies
;
;KiXtart:
;		Developed and tested KiXtart 4.61 and Windows 7 Ultimate.
;
;Example(s):
;		Break on
;		$rc = IsNumeric("2000")
;		? $rc
;
;Code:
Function IsNumeric($strInput)
	Dim $sc, $rc
	$sc = CreateObject("ScriptControl")
	$sc.Language = "VBScript"
	$rc = $sc.Eval('IsNumeric("' + $strInput + '")')
	If $rc = -1
		$IsNumeric = 1
	Else
		$IsNumeric = 0
	EndIf
EndFunction
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#203298 - 2011-10-20 03:22 PM Re: Validate a variable contains numbers [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Golf version, with validation tests and example to confirm length.
 Code:
Function IsNum($_)

  $IsNum = IIF($_ = Val($_), 1, 0)
  Exit 0

EndFunction

'123 ' IsNum('123') ?
'abc ' IsNum('abc') ?
'a23 ' IsNum('a23') ?
'1a3 ' IsNum('1a3') ?
'12a ' IsNum('12a') ?
'a2a ' IsNum('a2a') ?


$Id = '13293'
If Len($Id) = 5 And IsNum($Id)
  '5 digits!'
Else
  'invalid ID!'
EndIf
Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#203353 - 2011-10-24 05:26 PM Re: Validate a variable contains numbers [Re: Glenn Barnas]
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
 Code:
if len(val($number)) = 5
	? 'its a 5 dig number'
else
	? 'not a 5dig number'
endif




Edited by Bryce (2011-10-24 05:26 PM)

Top
#203370 - 2011-10-25 03:13 PM Re: Validate a variable contains numbers [Re: Bryce]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Bryce,

This won't work if the ID string is 5 digits followed by an alpha. "12345" will work but "12345a6" will return true when in fact it doesn't meet the criteria. That's why I compare the original against the Val() in my example - it makes sure that the original value is unchanged by the numeric conversion.

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

Top
#203372 - 2011-10-25 04:24 PM Re: Validate a variable contains numbers [Re: Glenn Barnas]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Glenn,

what if $uid = "01234" ?

even more golf here:

$IsNum = IIF($_ = 0+$_, 1, 0)
_________________________



Top
#203391 - 2011-10-26 04:10 PM Re: Validate a variable contains numbers [Re: Jochen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
That doesn't work.. I think we'll need some additional logic for leading zeros. \:\(

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

Top
#203392 - 2011-10-26 04:50 PM Re: Validate a variable contains numbers [Re: Glenn Barnas]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Well it's not as pretty, but this one seems to work.
 Code:
? isNum("01234")
? isNum("12345")
? isNum("01230")
? isNum("0123a")
? isNum("a1234")

get $

Function isNum($n)
   Dim $x,$c,$count
   For $x=1 to Len($n)
      $c = SubStr($n,$x,1)
      If $c = "0" OR Val($c)
         $count = $count+1
      Endif
   Next
   $isNum = IIF(Len($n)=$count, 1, 0)
EndFunction

Top
#203400 - 2011-10-26 05:37 PM Re: Validate a variable contains numbers [Re: ShaneEP]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
This:
 Code:
; Validate numeric string, strip leading zeros
Function IsNum($_)
  While Left($_, 1) = 0
    $_ = SubStr($_, 2)
  Loop
  $IsNum = IIF($_ = val($_), 1, 0)
  Exit 0
EndFunction

; validate string is numeric and 5 digits
Function IsNum3($_Id)

  If Len($_Id) = 5 And IsNum($_Id)
    '5 digits!'
  Else
    'invalid ID!'
  EndIf
  Exit 0
EndFunction

; Test various combinations
'  12345 ' IsNum3('12345') ?
'12345a6 ' IsNum3('12345a6') ?
'  abcde ' IsNum3('abcde') ?
'  a2345 ' IsNum3('a2345') ?
'  1a345 ' IsNum3('1a345') ?
'  1234a ' IsNum3('1234a') ?
'  a2a4b ' IsNum3('a2a45') ?
'  02468 ' IsNum3('02468') ?
works with leading zeros.. IsNum3 simply checks IsNum() and verifies that the length is 5. These could be combined pretty easily by adding an additional $_Length parameter to IsNum().

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

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 525 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.069 seconds in which 0.035 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