Search code examples
macosapplescriptosx-lionosx-mountain-lion

Can I restrict user from not entering special characters in dialogue boxes of applescript?


I have a requirement where I have to restrict user from entering the special characters in the dialog boxes where he ned to enter something. I want to allow only numbers and characters. Can anyone tell me whether it is possible or not??

The code goes like this:

display dialog "Enter You Name: " default answer "Name" buttons{"Proceed"}

In the default answer place user should not enter any special characters.. Can I restrict him by doing so??


Solution

  • You can try something like this:

    set allowedLetters to characters of (do shell script "printf \"%c\" {a..z}")
    set allowedNumbers to characters of (do shell script "printf \"%c\" {0..9}")
    set allowedAll to allowedLetters & allowedNumbers & space
    
    repeat
        set response to text returned of (display dialog "Enter You Name: " default answer "Name" buttons {"Proceed"} default button 1)
        try
            repeat with aCharacter in response
                if (aCharacter as text) is not in allowedAll then
                    display dialog "Please enter letters and numbers only" buttons {"OK"} cancel button 1 default button 1
                end if
            end repeat
            exit repeat
        end try
    end repeat
    
    return response