How to validate user input as integer, then confirm that integer is a specific length using Apple Script
Solution 1:
set input to &quot;123456&quot;

if the length of the input ≠ 6 ¬
    then return &quot;Wrong number of characters.&quot;

try
    if &quot;0123456789&quot; 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 &quot;Invalid characters.&quot;
end try



text -6 thru -1 of (&quot;000000&quot; &amp; 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 &quot;validate user input as integer&quot;, 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 &quot;Check the string is 6 characters and each of those is in range 0-9.&quot;  So that's exactly what this solution does, in one simple line:
set input to &quot;123456&quot;

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

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