flatiron / prompt

a beautiful command-line prompt for node.js

Home Page:http://github.com/flatiron/prompt

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to check a prompt before accepting it?

pbie42 opened this issue · comments

Is there a way that I can check an entered prompt before accepting it and if it does not fit ask the user to enter something else? For example I am using the prompt to have a restaurant server enter food items that have been ordered. I want to check the entered prompt to make sure what has been entered is on the current menu. I'm picturing this similar to when an entered prompt does not fit a specified pattern. Thank you in advance for your help!

I know you can use a regex, but that would probably not be the solution you are looking for. I would be nice if we could have a validation function that would reprompt the user if it doesn't fit certain criteria defined by that function. Like if I want a number that is a multiple of 3, I don't want to create a regex to test if the number is a multiple of three, I could just use a validation function reprompts the user if result % 3 !== 0

It's sort of hidden in the documentation, but you can do that.

  prompt.get([{
    name: 'username',
      required: true
    }, {
      name: 'password',
      hidden: true,
      conform: function (value) {
        return true;
      }
    }], function (err, result) {
    // 
    // Log the results. 
    // 
    console.log('Command-line input received:');
    console.log('  username: ' + result.username);
    console.log('  password: ' + result.password);
  });

You can provide a conform function that returns true/false if the entered values matches.