Prompt user for numbers only (integer?)

Prompt user for numbers only (integer?)

How can I get the user to only input numbers, so that there isn't errors when you input for instance a number?

I'm a total noob, sorry for the dumb question!

Prompt for User Input.kmactions (809 B)

Hey Gideon,

Not dumb at all. You just need to learn your way around Keyboard Maestro, and that takes time plus effort.

You cannot limit the input in a Prompt for User Input action. Everything is text.

You can validate the prompt after the user fills it in, and redisplay it for editing.

I'm using a regular expression to do this in the second action below.

Keyboard Maestro Actions.kmactions (3.3 KB)

You could even put that in a loop that would continue until the user provided the correct input.

The only way to force the correct input in the user-prompt would be to construct and use a Custom HTML Prompt action instead of using a plain old Prompt for User Input action -- but that's pretty advanced usage.

-Chris

and you could also use an IF THEN ELSE branch on a value returned by the built-in JavaScript isNaN function.

(It provides a broader check on whether a string is numeric – integer, decimal, scientific notation)

Check that input is numeric.kmmacros (24.7 KB)
mac2

(() => {
    'use strict';

    const main = () =>
        validatedLR(
            Application('Keyboard Maestro Engine')
            .getvariable('inputString')
        );


    // VALIDATED NUMBER STRINGS ---------------------------

    // validatedLR :: String -> Either String Num
    const validatedLR = s =>
        !s || isNaN(s) ? (
            Left(`Expected a number, but saw: '${s}'`)
        ) : Right(parseFloat(s, 10));

    // GENERIC FUNCTIONS ----------------------------------

    // https://github.com/RobTrew/prelude-jxa

    // Left :: a -> Either a b
    const Left = x => ({
        type: 'Either',
        Left: x
    });

    // Right :: b -> Either a b
    const Right = x => ({
        type: 'Either',
        Right: x
    });

    // showJSON :: a -> String
    const showJSON = x => JSON.stringify(x, null, 2);

    // MAIN ---
    return main();
})();

Super thanks, used Ccstone's suggestion, just put it into a loop :slight_smile:
Thanks you guys

1 Like