代码之家  ›  专栏  ›  技术社区  ›  Andrew Simpson

正则表达式\w不工作

  •  -1
  • Andrew Simpson  · 技术社区  · 6 年前

    我正在开发一个asp.net核心应用程序。

    我已经确认了。。

    我试图禁止用户只进入空间。所以我用这个:

        [Display(Name = "Reason for Cancellation")]
        [Required]
        [RegularExpression(@"\w", ErrorMessage = CancellationValidationErrorMessage)]
        [StringLength(245)] // 245 characters to allow for History Type prefix to be added
        public string CancelJustificationComments { get; set; }
    

    当错误弹出,说明我必须提供一个条目时,我也会得到其他输入的错误。

    怎么了?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Hans Kesting    6 年前

    regex验证器要求整个文本与regex匹配。因此此项只匹配一个字符项。

    解决方案:在其周围添加一些通配符: .*\w.*