locked
simple validation fails RRS feed

  • Question

  • User379720387 posted

    dur = "45" on postback

    It fails all three of the validation requirements. But it shouldn't...

    1. data was entered

    2. the stringlength =2

    3. 45 is an integer

    What am I doing wrong here?

    if (IsPost)
        {
            dur = Request["duration"];
            Validation.RequireField("dur", "duration is required");
            Validation.Add("dur",
                        Validator.StringLength(
                            maxLength: 3,
                            minLength: 1,
                            errorMessage: "Not a valid duration."));
            Validation.Add("dur",
                        Validator.Integer("Not a valid number"));
            
            if (Validation.IsValid())
            {
    
            }

    Wednesday, December 2, 2015 7:20 PM

Answers

  • User-821857111 posted

    The validators take the name of the form field, not the name of the variable that you have assigned the form field's value to.

    if (IsPost)
        {
            dur = Request["duration"];
            Validation.RequireField("duration", "duration is required");
            Validation.Add("duration",
                        Validator.StringLength(
                            maxLength: 3,
                            minLength: 1,
                            errorMessage: "Not a valid duration."));
            Validation.Add("duration",
                        Validator.Integer("Not a valid number"));
            
            if (Validation.IsValid())
            {
    
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 2, 2015 8:13 PM

All replies

  • User-821857111 posted

    The validators take the name of the form field, not the name of the variable that you have assigned the form field's value to.

    if (IsPost)
        {
            dur = Request["duration"];
            Validation.RequireField("duration", "duration is required");
            Validation.Add("duration",
                        Validator.StringLength(
                            maxLength: 3,
                            minLength: 1,
                            errorMessage: "Not a valid duration."));
            Validation.Add("duration",
                        Validator.Integer("Not a valid number"));
            
            if (Validation.IsValid())
            {
    
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 2, 2015 8:13 PM
  • User379720387 posted

    Of course. Thanks.

    Wednesday, December 2, 2015 9:40 PM