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

时间:2017-12-22 23:03:04

标签: c# asp.net asp.net-mvc forms

我一直在搜索google并寻找一种方法,我可以将正则表达式或某种属性添加到字符串中,如果用户在特殊字符位于所述字符串中时尝试提交表单,则会显示错误消息? (类似于必需的属性)

前:

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

C#Class:

  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没有找到任何有用的信息,所以任何有关尝试的提示或有用的评论都会很棒,谢谢!

1 个答案:

答案 0 :(得分:4)

您可以使用RegularExpressionAttribute class来限制允许的内容,而不是正则表达式。