locked
CustomValidation string length either 6 or 13 RRS feed

  • Question

  • User-1952516322 posted

    Hello,

    I want to know how to build a class example (CustomStringLength) to validate a string if the length is 6 or 13.

    Note: I don't want a value between or not range validation ,, just the correct value if the length is 6 or 13.

    Wednesday, July 3, 2019 12:41 PM

Answers

  • User-474980206 posted

    simple regex validator:  

       ^.{6}$|.{13}$

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2019 1:51 PM
  • User1520731567 posted

    Hi Khalid Salameh,

    As @bruce said,you could use regex to achieve your need simply.

    Khalid Salameh

    I want to know how to build a class example (CustomStringLength) to validate a string if the length is 6 or 13.

    And you could also use custom validate,for example:

    Model:

        [CustomStringLength(ErrorMessage = "The field length only should be 6 or 13")]
        public string Name { get; set; }

    CustomStringLength class:

     public class CustomStringLengthAttribute : ValidationAttribute
        {
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                if (value.ToString().Length!=6 && value.ToString().Length!=13)
                {
                    return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
                }
                return null;
            }
        }

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 4, 2019 4:42 AM

All replies

  • User-474980206 posted

    simple regex validator:  

       ^.{6}$|.{13}$

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2019 1:51 PM
  • User1520731567 posted

    Hi Khalid Salameh,

    As @bruce said,you could use regex to achieve your need simply.

    Khalid Salameh

    I want to know how to build a class example (CustomStringLength) to validate a string if the length is 6 or 13.

    And you could also use custom validate,for example:

    Model:

        [CustomStringLength(ErrorMessage = "The field length only should be 6 or 13")]
        public string Name { get; set; }

    CustomStringLength class:

     public class CustomStringLengthAttribute : ValidationAttribute
        {
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                if (value.ToString().Length!=6 && value.ToString().Length!=13)
                {
                    return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
                }
                return null;
            }
        }

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 4, 2019 4:42 AM
  • User-1952516322 posted

    Hello Yuki, bruce

    Thanks for your reply, but I want to know if there is a class like an example >> public class CustomLength: StringLengthAttribute,IClientValidatable

    I want to do like this

    [CustomLength(6,13)]
    public string Name

    Is there a class I can build constructor got two value,     the (StringLengthAttribute) take just minimum value, but I don't want minimum or max because it's not range, I want like the code above, please if someone know help me.

    Thursday, July 4, 2019 6:45 AM