Regex is not working in RegularExpression attribute on c#

这一生的挚爱 提交于 2020-01-05 07:20:21

问题


guys, I don't know this question is already present or not but I have tried every search, so my question is why my regex is not working properly in RegularExpression attribute. this same regex I have used in javascript and this is working on javascript. can anyone help me what I am doing wrong here?

[Required]
[Display(Name = "First name")]
[MaxLength(50)]
[RegularExpression("^(?![@\\+\\-=\\*])", ErrorMessage = "First Name Should not start with these characters @, +, =, *, -")]
public string firstname { get; set; }

I am using this regex for validating the First Name should not start with @,+,=,*,-.

I have already spent 3 hours to figure out what I am doing wrong here.


回答1:


I believe you regex should look like this:

^(?![@\\+\\-=\\*]).*

Here is a working example.




回答2:


Your regex is invalid. Here is the updated one, which works as you expected:

^(?![@\\+\-\\=\\*])


来源:https://stackoverflow.com/questions/48295781/regex-is-not-working-in-regularexpression-attribute-on-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!