Share via


Validation for max and min length of an int

Question

Wednesday, June 6, 2012 6:24 PM

Hello,

I want to make sure the user does not input a int with a length more then 6 and less then 6.

I know you can use StringLength for strings, but how would I do this for ints?

 [Required(ErrorMessage = "Please Enter your Badge ID")]
        [StringLength(6, MinimumLength = 6)]
        public int BadgeID { get; set; }

Thanks in advance

All replies (11)

Wednesday, June 6, 2012 6:29 PM ✅Answered

Perhaps this?

[Range(100000, 999999)]

public int BadgeID {get;set;}

Seems like a hack and I don't like it. Also maybe you could come up with a regex and use [RegularExpression] instead.


Thursday, June 7, 2012 1:05 PM ✅Answered

You can use regular expressions to prevent alphatical characters from being valid input

[RegularExpression(@"([0-9]+)", ErrorMessage = "Must be a Number.")]

Wednesday, June 6, 2012 6:35 PM

I don't think we have any out of box solution for that, if available then I might not be aware. You can implement a custom validator attribute for that. Something like below, though it is not complete or you can use a regular expression validator.

public class IntLengthValidationAttribute : ValidationAttribute { 
    public override bool IsValid(object value) { 
        if (value == null) { 
            return true; 
        } 
        string convertedValue = value.ToString();

        if(ValidateRange(convertedValue))
          return true;        

        return false; 
    } 
} 

Thanks, 

 


Wednesday, June 6, 2012 7:44 PM

Perhaps this?

[Range(100000, 999999)]

public int BadgeID {get;set;}

Seems like a hack

A hack? This seems to me a standard validation rule, which can be implemented using a rangevalidator...


Wednesday, June 6, 2012 7:55 PM

Yea, I know it works... still leaves me with an uneasy feeling :P


Thursday, June 7, 2012 11:54 AM

Thank you , this worked as of now. :)


Thursday, June 7, 2012 12:20 PM

Yea, I know it works... still leaves me with an uneasy feeling :P

Haha, I know what you mean (I lol'd a bit as well).  Especially in the context of "badge ids".  What if someone wanted the id 000105.  That's technically no more or less than 6 characters, but the server will read that int value as 105, not 000105.  And validation is going to respond back saying the value must be between 100,000 and 999,999, which just seems odd.... and possibly confusing to the user.

The badge id may need to be of type string if zero's are allowed as the starting character of the id.


Thursday, June 7, 2012 12:25 PM

If I changed the badge ID to be a string, how would I check to make sure they enter in a number value?


Thursday, June 7, 2012 1:09 PM

Oh okay, thanks a ton :)


Thursday, June 7, 2012 3:50 PM

but the server will read that int value as 105, not 000105

There's a difference in how you store values, and how you display values.


Friday, June 8, 2012 8:09 AM

There's a difference in how you store values, and how you display values.

There certainly is, but I don't think that's the case considering he's requiring a minimum 6 digits.  He could just as easily use the maximum ceiling, then introduce his own logic in the output display to add zeros to the beginning should someone's badge id not be 6 digits in length.