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:
-
User can't enter letters or symbols.
-
Input must be in an integer (whole numbers only, and yes zero counts).
-
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?
Best Answer
Solution 1:
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:
The variable
validation
will either contain the 6-digit code if it is valid, or"Invalid input"
otherwise.