正则表达式在C#的RegularExpression属性中不起作用 [英] Regex is not working in RegularExpression attribute on c#

查看:461
本文介绍了正则表达式在C#的RegularExpression属性中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我不知道这个问题是否已经存在,但是我已经尝试了所有搜索,所以我的问题是为什么我的正则表达式不能在RegularExpression属性中正常工作.我在javascript中使用过的同一个正则表达式,正在javascript上运行.有人可以帮我在这里做错什么吗?

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 @,+,=,*,-.

我已经花了3个小时来弄清楚我在这里做错了什么.

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

推荐答案

我相信您的正则表达式应如下所示:

I believe you regex should look like this:

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

这是一个有效的示例.

这篇关于正则表达式在C#的RegularExpression属性中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆