0

I'm building an Apple Script to prompt and receive input from a user via a dialog box. The default answer of the dialog is "Enter Code". The required input is a 6-digit code given to each user. On receiving the user's input (or code), some validation checks need to occur confirming that the user's input is acceptable based on the following conditions:

  1. User can't enter letters or symbols.

  2. Input must be in an integer (whole numbers only, and yes zero counts).

  3. The code (input), just be exactly (or equal to) 6 digits long. No more or no less than 6.

Background - the user's code is generated ad-hoc from another application and is unique each time. There is no way to cross reference this code.


For example, the user enters 123456 into the dialog box. Using Apple Script, how can I script this, ensuring again, that the code is all numbers, and precisely 6 digits long?

  • What have you experimented with in Automator to attempt the validation, but it has failed? – IconDaemon Jul 31 '18 at 16:42
  • What happens if the code begins with 0? – Mark Jul 31 '18 at 17:54
  • Basically this code should have nothing to do with numeric types. Check the string is 6 characters and each of those is in range 0-9. In this case a regex might not add to the problems. – Mark Jul 31 '18 at 18:32
1

Solution 1:

set input to "123456"

if the length of the input ≠ 6 ¬
    then return "Wrong number of characters."

try
    if "0123456789" does not contain item 1 of the input ¬
        or item -1 of the input is in [space, tab, linefeed] ¬
        then error

    set input to input as number

    if class of the input ≠ integer then error
on error
    return "Invalid characters."
end try



text -6 thru -1 of ("000000" & the input)

Solution 2:

This solution benefits from being extremely short, but also treats the problem as it ought to be treated. It's a misnomer to say you wish to "validate user input as integer", when, in fact, we are only ever dealing with text. The passcode is a 6-character passcode, and those characters are limited to the unicode values that represent digits; but they are still text characters, and not integers in any numerical sense.

Mark's comment against the question actually stated this, but even I fell into the mindset of wanting to assess the input as numbers, which is partly what Solution 1 above does; and it works, and it is a perfectly good solution, but it performs needless steps to get to the end result.

Treating the input purely as text, the problem, so succinctly stated by Mark is to "Check the string is 6 characters and each of those is in range 0-9." So that's exactly what this solution does, in one simple line:

set input to "123456"

set validation to do shell script ¬
    "egrep -x '[0-9]{6}' <<<" & ¬
    quoted form of the input & ¬
    "|| echo Invalid input"

The variable validation will either contain the 6-digit code if it is valid, or "Invalid input" otherwise.

0
display dialog "Enter Your Code" default answer "" with hidden answer # Ask for user input as dots instead of text
set myCode to the text returned of the result as string # set myCode to what the user enters
set isNum to true # define isNum as true unless changed
try
    set bla to myCode as number # will fail if myCode is not a number
on error
    set isNum to false # failed
end try
set prohibitedChars to {".", " ", "+", "-"}
set myItems to every item of myCode
set containsGoodChars to true
repeat with i in myItems
    if i is in prohibitedChars then
        set containsGoodChars to false
    end if
end repeat
if ((length of myCode is 6) and (isNum) and (containsGoodChars)) then # if myCode is 6 characters long and it is a number then

    --do some code

else # validation failed
    display dialog "You failed. And so did the validation."
end if
  • no you have not fixed leading 0 or -.or + . Baically converting to a numeric for a code is wrong – Mark Jul 31 '18 at 18:37
  • @Mark I think this ones good now. Leading 0 is allowed. – JBis Jul 31 '18 at 18:42
  • I feel that, had you considered more carefully the order in which you perform your validation checks, the resulting code would have turned out a lot more pleasing. – CJK Aug 1 '18 at 1:45
  • @CJK In what order would you have suggested it? – JBis Aug 1 '18 at 1:46
  • 1
    Good question, probably with more than one solution. But I think the first check is crucially the length of the input; and the last (or one of the last) checks is that it coerces to a number. Also, if one check fails, the remaining checks are superfluous; however, if your code fails to coerce to a number, it continues checking other criteria (although I recognise there could be situations where this would be useful, namely if we wanted to know a breakdown of all errors found in the user's input). – CJK Aug 1 '18 at 1:52

You must log in to answer this question.

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