How to use ValidationAttribute for user input in options pages?

泪湿孤枕 提交于 2019-12-11 08:34:23

问题


I'm writing a Visual Studio extension. I have a C# class representing an options page. An option is represented by a public property, like this:

public class OptionsPageGeneral : DialogPage
{
  [Range(1, 100)]
  [Category("Misc")]
  [DisplayName("Integer option")]
  public string IntOption { get; set; }
  ...
}

I'm trying to use the RangeAttribute validation attribute to restrict user input. However, for the given code, user still can input any value, not only from [1; 100] range.

I've seen lots examples of ValidationAttribute usage, but all for ASP.NET MVC projects. Is it true this attribute is only applicable in that context?

Anyway, how can I validate user input done in options page? I know I can simply override the property set method, but validation attributes require much less code to write and can be reused for the similar properties.


回答1:


I dont have idea on how to use options page, but you can use a regular expression if you want a range between 1-100 instead of Range.

  [Category("Misc")]
  [DisplayName("Integer option")]
  [RegularExpression(@"[0-9]{2}")]//This will only allow you to enter only 2 digits
  public string IntOption { get; set; }


来源:https://stackoverflow.com/questions/12044509/how-to-use-validationattribute-for-user-input-in-options-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!