How can i check if the lists item is a character or a number ?

How can i check if the lists item is a character or a number ?
I know that the a-zA-Z part is not correct, but i can’t figure out how to do it right.
Only a-z A-Z 0-9 and space should be allowed in the string the user entered.

And if you think that the way I try to solve isn’t the best, tell me. :wink:


display dialog "Enter name of new setup:" default answer ¬
	"" buttons {"OK", "Cancel"} default button 1 with icon note

set SetupName to text returned of result
set SetupNamelen to (length of SetupName)

if SetupNamelen is less than 1 then
	display dialog "You didn't enter a name for the setup.¬
	 Please press \"Add New Setup\" again and enter a name¬
	  next time." buttons {"OK"} default button 1
	
else if SetupNamelen is greater than 0 then
	set setupnameitems to text items of SetupName
	repeat with i from 1 to SetupNamelen
		
		if item i of the setupnameitems is not equal to ([a - zA - Z0 - 9]) then -- <--- doesn't work - zA and Z0 are variables after compiling - which is not what i wanted ;)
			
			display dialog "Only letters, numbers and space allowed!"
			set addokay to false
		else
			set addokay to true
		end if
		
	end repeat
end if

AppleScript: 1.10.3
Browser: Firefox 1.5
Operating System: Mac OS X (10.4)

set validCharacters to "abcdefghijklmnopqrstuvwxyz1234567890 "

set theInput to (text returned of (display dialog "The Input:" default answer "")) as string

set theInputIsValid to true
if theInput is "" then
	set theInputIsValid to false
else
	repeat with tmpChar in characters of theInput
		--considering case
		if validCharacters does not contain tmpChar then
			set theInputIsValid to false
			exit repeat
		end if
		--end considering
	end repeat
end if

if theInputIsValid then
	display dialog ("Valid Input: \"" & theInput & "\"")
else
	display dialog "Invalid Input!"
end if

Note that applescript is not case sensitive by default, so if you want to add case sensitivity you’ll have to use “considering case” as I’ve commented in the above script.

j

Thank you. This works perfectly. :slight_smile:

AppleScript: 1.10.3
Browser: Firefox 1.5
Operating System: Mac OS X (10.4)