简体   繁体   English

ASP.NET是否可以添加不允许在字符串中使用特殊字符的属性?

[英]ASP.NET is it possible to add an attribute that doesn't allow special characters in a string?

I have been scavenging google and looking for a way that I can add regular expressions or some sort of attribute to a string that would show an error message if the user tries to submit the form while special characters are in said string? 我一直在寻找Google,并寻找一种可以向字符串添加正则表达式或某种属性的方法,如果用户在字符串中包含特殊字符时尝试提交表单,该字符串会显示错误消息? (similar to the required attribute) (类似于必填属性)

Ex: 例如:

Allowed -> Username
Not Allowed -> User'Name,&

C# Class: C#类:

  public partial class Client
    {
        public int CID { get; set; }
        [Required(ErrorMessage ="This field is Required")]
        public string Company { get; set; }

        [Required(ErrorMessage = "This field is Required")]
        public string Email { get; set; }

        [Required(ErrorMessage = "This field is Required")]
        public string Location { get; set; }

        [Required(ErrorMessage = "This field is Required")]
        public string Phone { get; set; }

        [Required(ErrorMessage = "This field is Required")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Required(ErrorMessage = "This field is Required")]
        [DisplayName("Confirm Password")]
        [Compare("Password")]
        public string ConfirmPassword { get; set; }

        [DefaultValue(false)]
        [DisplayName("Special Client")]
        public Nullable<bool> Special { get; set; }

        [Range(0, int.MaxValue, ErrorMessage = "The value must be greater than 0")]
        [DisplayName("Monthly Purchased Hours")]
        public int HoursPayed { get; set; }

        public string LoginErrorMessage { get; set; }

Haven't found anything useful so far so any tips or helpful comments on what to try would be awesome, thanks! 到目前为止,还没有发现任何有用的东西,因此,关于尝试什么的任何提示或有用的评论都将非常棒,谢谢!

您可以使用RegularExpressionAttribute类来限制正则表达式所允许和不允许的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM