data validation on prompt Input box on Windows Phone

602 Views Asked by At
RadInputPrompt.Show("Enter the number", MessageBoxButtons.OK, message, InputMode.Text, textBoxStyle, closedHandler: (arg) =>
{
    int okButton = arg.ButtonIndex;
    if (okButton == 0) 
    {

          //do some check before submit
          if (string.IsNullOrEmpty(arg.Text))
          {
               MessageBox.Show("Please input the number.");
               return; //??
          }

          //submit
    }
    else
    {
        return;
    }
});

My question is : I do some data validation (for example: numeric only, the digit count...) before submit

If the input from user is invaild, I hope the Prompt Input Screen can still remain.
If I use "return" keyword, it'll go back to the main screen.

Or is there any other ways of validation (something like AJAX?) that I can use on this prompt sceen rather than do it on code-behind page?

Thanks a lot!

1

There are 1 best solutions below

0
On

One technique is to just keep looping and showing the input prompt each time the user clicks OK, but fails to satisfy the input validation. You can see an example of this below with the input text box continuing to repeat if the result is not a valid numeric value.

It's also a good idea to add some kind of feedback to the user indicating that the previous input was not acceptable in the event of an invalid submission. An example of this is below where the title of the input text box is changed after the first invalid submission to include text indicating that the input value must be a valid number.

NOTE: Telerik is saying the ShowAsync method should now be used instead of the Show method since it is being deprecated.

        string userInput = string.Empty;
        int okButton = 0;
        bool firstPass = true;
        double numericResult;

        while (okButton.Equals(0) && string.IsNullOrWhiteSpace(userInput))
        {
            string inputBoxTitle = (!firstPass) ? "Enter the number (you must enter a valid number)" : "Enter the number";

            InputPromptClosedEventArgs args = await RadInputPrompt.ShowAsync(inputBoxTitle, MessageBoxButtons.OKCancel);
            okButton = args.ButtonIndex;
            firstPass = false;

            if (okButton.Equals(0))
            {
                if (!string.IsNullOrWhiteSpace(args.Text))
                {
                    bool isNumeric = double.TryParse(args.Text, out numericResult);
                    if (isNumeric)
                    {
                        // We have good data, so assign it so we can get out of this loop
                        userInput = args.Text;
                    }
                }
            }
        }