正则表达式用于验证姓名的C#:

huangapple go评论62阅读模式
英文:

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., '-]+"

huangapple
  • 本文由 发表于 2023年3月12日 16:05:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定