Checking that the text returned from a dialog is only number

Hi all,

I am trying to code this and I am totally stuck!
I need this code to check that the information typed in the dialog text box is only numbers, 10 digits long, and is not the default value.

I have done the latter two but cannot figure the first one. Here is my current code.

set theTextISBN to text returned of dialogResultISBN --coercion ISBN to text
		if the theTextISBN != "01701*****" and (count theTextISBN) = 10 then
			exit repeat
		end if

Currently this does not ensure that the returned value is only digits I only put it in to aid anyone who may consider helping me! :slight_smile:

Thanks in advance,

Dave.

I’d probably use something like this. You can change one of the digits to a letter to see how it works.

set x to "7463535546"

try
	x as number
	if (count of x) is 10 then
		display dialog x & " is a number with 10 digits"
	end if
	-- do something with the number
on error
	display dialog x & " is not a number"
	-- do something with the non-number
end try

Sorry I don’t think I completely understand. Maybe posting the whole segment of script helps?

The idea was that if the 10 characters are indeed digits it exits the loop, otherwise it repeats the loop and makes the user reenter the information.

	repeat
		try
			set dialogResultISBN to (display dialog "What is the ISBN ? 
10 digits with no spaces or dashes
     Max    
" default answer "01701*****" with icon 1)
		on error errMessage number errNum
			if errNum = -128 then
				display dialog "The user hit cancel!" buttons {"Exit"} default button 1
				if button returned of result = "Exit" then
					error number -128
				end if
			else
				display dialog "An error occurred: " & errMessage
			end if
		end try
		
		set theTextISBN to text returned of dialogResultISBN as integer --coercion ISBN to text
		if the theTextISBN != "01701*****" and (count theTextISBN) = 10 then
			exit repeat
		end if
	end repeat

I’m sorry I just did not understand how I incorporated that last segment of code you gave me into the script. :frowning:

Maybe this will work. :slight_smile:

set is10Digits to false
set isNumber to false

repeat until isNumber is true and is10Digits is true
	
	try
		set dialogResultISBN to (display dialog "What is the ISBN ? 
10 digits with no spaces or dashes 
     Max    
" default answer "01701*****" with icon 1)
		
	on error errMessage number errNum
		if errNum = -128 then
			display dialog "The user hit cancel!" buttons {"Exit"} default button 1
			if button returned of result = "Exit" then
				error number -128
			end if
		else
			display dialog "An error occurred: " & errMessage
		end if
	end try
	
	
	set theTextISBN to text returned of dialogResultISBN
	try
		theTextISBN as number
		set isNumber to true
		
		if (count of theTextISBN) is 10 then
			set is10Digits to true
		end if
	on error
		set isNumber to false
		set is10Digits to false
	end try
end repeat

hi again,

THANKYOU!!! The script worked perfectly and seamlessly!

Thanks for the effort you spent in writing it.

Dave :smiley: :smiley: :smiley: :smiley: :smiley: :smiley:

Dave, what you were missing from Rob’s post is a very suble, but clever trick, namely the line:

x as number

This makes AppleScript coerce (convert) the string into a number. This can only be done if it only contains digits*. If any non-digits exist, the coercion will fail and you’ll drop into the ‘on error’ part of the try/end try statement.

For example, if the string was “ABCDE”, you can’t coerce that to a number so the error statement "display dialog (x & “is not a number”) runs.

  • The exception to this coercion rule is the “-” sign… e.g. “-11243598” is a valid number and would pass Rob’s code. Therefore you’d need to add additional code to eliminate negative numbers.

Oooops! I didn’t consider that! Thanks for the heads up. It took only one additional line to fix it. Here’s the fixed version:

set is10Digits to false
set isNumber to false

repeat until isNumber is true and is10Digits is true
	
	try
		set dialogResultISBN to (display dialog "What is the ISBN ? 
10 digits with no spaces or dashes 
     Max    
" default answer "01701*****" with icon 1)
		
	on error errMessage number errNum
		if errNum = -128 then
			display dialog "The user hit cancel!" buttons {"Exit"} default button 1
			if button returned of result = "Exit" then
				error number -128
			end if
		else
			display dialog "An error occurred: " & errMessage
		end if
	end try
	
	
	set theTextISBN to text returned of dialogResultISBN
	try
		theTextISBN as number
		set isNumber to true
		
		if character 1 of theTextISBN is "-" then set isNumber to false
		
		if (count of theTextISBN) is 10 then
			set is10Digits to true
		end if
	on error
		set isNumber to false
		set is10Digits to false
	end try
end repeat

I haven’t studied the script closely, but if “-” has to be checked for, how about “.”? After all, “1234.56789” can also be coerced into a number.

If I missed something in the posted code, disregard this post. :smiley:

Brad Bumgarner, CTA

Good call! Here’s the fix. :slight_smile:

set is10Digits to false
set isNumber to false

repeat until isNumber is true and is10Digits is true
	
	try
		set dialogResultISBN to (display dialog "What is the ISBN ? 
10 digits with no spaces or dashes 
     Max    
" default answer "01701*****" with icon 1)
		
	on error errMessage number errNum
		if errNum = -128 then
			display dialog "The user hit cancel!" buttons {"Exit"} default button 1
			if button returned of result = "Exit" then
				error number -128
			end if
		else
			display dialog "An error occurred: " & errMessage
		end if
	end try
	
	
	set theTextISBN to text returned of dialogResultISBN
	try
		theTextISBN as number
		set isNumber to true
		
		if character 1 of theTextISBN is "-" or theTextISBN contains "." then set isNumber to false
		
		if (count of theTextISBN) is 10 then
			set is10Digits to true
		end if
	on error
		set isNumber to false
		set is10Digits to false
	end try
end repeat

I little quicker solution would be:

theTextISBN as integer

Then you could check for the “-” as before.

on isPositiveInteger(str)
	-- O(n) algorithm is inefficient on long strings, but strings are likely to be short so not an issue.
	-- Not fastest solution possible, but very simple and robust - more than adequate for validating user input.
	if str's class is not in {string, Unicode text} then error "Not a string/unicode text." number -1703
	if str's length is 0 then return false
	considering case, diacriticals, expansion, hyphens, punctuation and white space
		repeat with charRef in str
			if charRef's contents is not in "0123456789" then return false
		end repeat
	end considering
	return true
end isPositiveInteger

on isISBN(str)
	if str's class is not in {string, Unicode text} then error "Not a string/unicode text." number -1703
	return str's length is 10 and isPositiveInteger(str)
end isISBN

isISBN("1826468312") --test

I wondered if you were going to check in on this topic! Nice handler! :slight_smile: