Regex is not working in RegularExpression attribut

2019-07-28 17:59发布

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.

2条回答
乱世女痞
2楼-- · 2019-07-28 18:16

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

^(?![@\\+\-\\=\\*])
查看更多
疯言疯语
3楼-- · 2019-07-28 18:18

I believe you regex should look like this:

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

Here is a working example.

查看更多
登录 后发表回答