简体   繁体   中英

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? (similar to the required attribute)

Ex:

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't found anything useful so far so any tips or helpful comments on what to try would be awesome, thanks!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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