如何验证对象是否只包含带有DataAnnotation的字母字符?

时间:2013-12-20 05:05:27

标签: c# string data-annotations system.componentmodel

我正在编写一些代码,用于验证用户是否已提交了包含正确信息的对象。换句话说,除了英文字母或拉丁外国元音(法语,德语等)之外,我如何验证该名称不包含数字,符号和其他符号?

    [Required]
    [MaxLength(50)]
    [DataType()] //I think a solution would use custom datatypes
    public string Name { get; set; }

1 个答案:

答案 0 :(得分:3)

您可以使用正则表达式:

[RegularExpression(@"^[\p{L}]+$")]

这基本上说“只允许整个事物中的任何Unicode字符”。