1

Can we check whether the input form in an adaptive card is filled or not with a warning message. I am currently using an adaptive card to gather user input in bot application,I have already added isRequired for input validation but it doesnot give any warning message instead when I click on submit it doesnot go to the next method. As soon as the user presses submit I want to make sure that the form is not empty

Noel Simon
  • 15
  • 7
  • 1
    Welcome in Stackoverflow. We’d love to help you. To get a good Answer for your Question: Can you edit the question and provide some code and/or more explanations, if there is, of what you are doing, and what's wrong. To avoid downvote and bad comments, please take some time to read Help: https://stackoverflow.com/help and How do I ask a good question: https://stackoverflow.com/help/how-to-ask – Shim-Sao Mar 29 '19 at 10:53
  • 2
    Not reliably, because the cards have to run in all kinds of different channels. The [docs for adaptive cards](https://learn.microsoft.com/en-us/adaptive-cards/rendering-cards/implement-a-renderer) say _"A renderer DOES NOT have to implement validation of the input. Users of Adaptive Cards must plan to validate any receieved data on their end … We do not make any promises of input validation in adaptive cards, so it's up to the receiving party to properly parse the response. E.g., a Input.Number could return "hello" and they need to be prepared for that."_ - – stuartd Mar 29 '19 at 10:55
  • Can we use regex to validate whether a person has entered a valid email or not in the input form@stuartd – Noel Simon Mar 29 '19 at 11:01
  • What channel are you using? – Kyle Delaney Mar 29 '19 at 19:32
  • microsoft teams@KyleDelaney – Noel Simon Apr 02 '19 at 11:38
  • Is my answer acceptable? – Kyle Delaney Apr 18 '19 at 02:30
  • yes@KyleDelaney – Noel Simon Apr 23 '19 at 07:10

2 Answers2

2

If you have an Adaptive Card like this (notice the ID given to the input):

var card = new AdaptiveCard
{
    Body =
    {
        new AdaptiveTextBlock("Adaptive Card"),
        new AdaptiveTextInput { Id = "text" },
    },
    Actions = {
        new AdaptiveSubmitAction { Title = "Submit" } },
    },
};

You can validate the value sent through the submit action like this:

if (string.IsNullOrEmpty(turnContext.Activity.Text))
{
    dynamic value = turnContext.Activity.Value;
    string text = value["text"];   // The property will be named after your input's ID
    var emailRegex = new Regex(@"^\S+@\S+$");   // This is VERY basic email Regex. You might want something different.

    if (emailRegex.IsMatch(text))
    {
        await turnContext.SendActivityAsync($"I think {text} is a valid email address");
    }
    else
    {
        await turnContext.SendActivityAsync($"I don't think {text} is a valid email address");
    }
}

Validating email with regex can get very complicated and I've taken a simple approach. You can read more about email Regex here: How to validate an email address using a regular expression?

Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66
1

I took totally different approach than the accepted answer here. If you are going to use adaptive cards a lot in your bot than it makes sense to create card models and have validation attributes applied to each field that needs validation. Create custom card prompt inheriting from Prompt<object> class. Override OnPromptAsync and OnRecognizeAsync and check the validation of each field there.

A.G.
  • 304
  • 2
  • 11