Hi,
How would I use AppleScript to check if a text field only contains numbers, and if it contents anything other than numbers an error is displayed?
Thanks
Hi,
How would I use AppleScript to check if a text field only contains numbers, and if it contents anything other than numbers an error is displayed?
Thanks
Hi,
you can just coerce the string to an integer.
It throws an error, if it’s no number
on checkInteger(x)
try
x as integer
return true
on error
return false
end try
end checkInteger
In one of my projects I use these handlers to check for number while the user is typing
The appropriate text field is connected to the on changed handler
property ciphers : "0123456789"
property maxLength : 3
on changed theObject
set textValue to content of theObject
set textValue to check_integer(textValue)
if textValue contains "´" or textValue contains "`" or textValue contains "^" or textValue contains "~" then set textValue to ""
set content of theObject to textValue
end changed
on check_integer(v)
tell v
try
if last character is not in ciphers then
if length is 1 then return ""
return text 1 thru -2
else
if length > maxLength then return ((text -maxLength thru -1 as integer) as text)
return (it as integer) as text
end if
on error
return ""
end try
end tell
end check_integer