How would I validate this pattern: 2 digits followed by last name, basically any length of letters. Apple script does not use regular expressions, correct?

I need to validate user input and all student user IDs on my campus are in that format.


    display dialog "Enter  user name:  (like 99smith)" default answer "" giving up after 40

    set {userName, returnedButton, gaveupBoolean} to the result as list
    -->{"some text", "OK", false}

    if userName is not "" then
        set CheckName to text 1 thru 2 of userName & " --test"

        display dialog CheckName & " -- 1"

        if class of CheckName is number then

            display dialog CheckName & " -- 2"
            exit repeat
            display dialog "Name does not start with 2 digits  " & CheckName & " -- " & userName
        end if

    end if

end repeat
  • Can you show us what you have tried? – AllInOne Jun 20 '14 at 17:30
  • The line CLASS of CheckName is NUMBER never seems true even when check name is 2 digits – Len Jun 23 '14 at 14:57
  • Please see my edit below. – AllInOne Jun 23 '14 at 19:31

Can you approach it like this:

Take the first two characters:

--test to see if they are numbers.
-->If not return invalid.

Take the remaining characters:

--test for minimum length?

--> if not return invalid.

--test each character to see if they are numbers.

--> If any character is a number return invalid.

Edit to add:

Rather than checking to see if CheckName is a number try converting it to a number:

    set CheckName to CheckNameDigits as number
on error
    display dialog CheckName & " is not a number"
end try

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .