Regex is not working in RegularExpression attribut

2019-07-28 18:30发布

问题:

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:

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