英文:
Reg Expression to validate name C#
问题
我尝试了许多不同的正则表达式来验证常见的姓名,但都没有成功。我只想验证并允许逗号、' - 和点。就这样。我正在使用 C# 在模型类中进行 MVC 开发。我尝试了下面的正则表达式并进行了不同的修改,但没有起作用。
[RegularExpression("[A-Za-z.,-' ]*$", ErrorMessage = "Not in correct format")]
public string FullName { get; set; }
请您也告诉我同样的正则表达式是否可以在 JavaScript 或 jQuery 中使用。
英文:
I tried many different set of regex to validate usual names but none worked.
I want to only validate and allow comma, ' - and . That all.
I am working on MVC using C# in Model class. I tired below Regex and modified it differently but it is not working.
[RegularExpression("[A-Za-z.,-' ]*$", ErrorMessage = "Not in correct format ")]
public string FullName { get; set; }
Please also let me know if the same can be use in javascript or jQuery.
答案1
得分: 0
你的主要问题是在[]
中将-
放在最后一个字符位置。它被解释为一个范围。在你的情况下,任何在,
和'
之间的字符,但是,
在ASCII中位于'
之后,所以没有字符。
我建议你使用以下正则表达式:
"[A-Za-z., '-]+"
英文:
Your main problem is having -
not as the last character in []
. It assumes it is a range. I your case any character between ,
and '
but ,
is after '
in ASCII so no characters.
I suggest you use the following:
"[A-Za-z., '-]+"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论