Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check a prompt before accepting it? #168

Open
pbie42 opened this issue Dec 1, 2016 · 2 comments
Open

How to check a prompt before accepting it? #168

pbie42 opened this issue Dec 1, 2016 · 2 comments

Comments

@pbie42
Copy link

pbie42 commented Dec 1, 2016

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!

@mdagaro
Copy link

mdagaro commented Dec 13, 2016

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

@omgimanerd
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants