Verifying a numeric string

How do I verify that a string from a dialog is all numeric? Thanks.

Either of these should work:

set s to "123456"

-- all AppleScript solution

set isNum to true
repeat with i from 1 to length of s
	set n to ASCII number (character i of s)
	if n < 48 or n > 57 then
		set isNum to false
		exit repeat
	end if
end repeat

-- Unix shell solution

set isAllNums to do shell script "echo " & s & "|sed 's/^[0-9]*$/yes/'"
if isAllNums = "yes" then
	-- it's all numbers
	-- it's not all numbers
end if
  • Dan

You can do something like this:

set theResult to display dialog "Input a Number" default answer "" buttons {"Cancel", "OK"} default button "OK"
	set theNumber to text returned of theResult as number
on error
	-- handle errror here
end try

Hope this helps,
Brad Bumgarner, CTA

yeah, I like that better…

  • Dan

Cool. :smiley: Thanks.