locked
validating an optional field RRS feed

  • Question

  • User-2060576634 posted

    hi everyone.. i want to validate an optional form field in a way that validation would be required only if the user decides to fill in the field.. this is the validation i desire:

                Validation.Add("phone",
                Validator.StringLength(
                maxLength: 11,
                minLength: 11,
                errorMessage: "(11 digits please)"));

    how should i code this type of validation?

    right now the validation prompts the user even if he decides to leave the field empty.

    Thursday, February 12, 2015 7:17 AM

Answers

  • User1713851783 posted

    Try with

    Validation.Add("phone", 
            Validator.Regex(@"^\d{11}$", 
            "(11 digits please)"));

    As is often the case, you can find a solution on the Mike Brind's site: Validation In  Razor Web Pages 2.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 12, 2015 11:45 AM