简体   繁体   English

自定义正则表达式以允许文本框中的字母和点

[英]Customize regular expression to allow alphabets and dots in textbox

Hello friends In my application I want to customize regular expression in such a way that it will allow character and dot only. 朋友您好在我的应用程序中,我希望以仅允许使用字符和点的方式自定义正则表达式。 For eg. 例如。 I could be be able to enter MCA or MCA any of these. 我可以输入以下任意一项的MCA或MCA。 For this task how to customize regular expression? 对于此任务,如何自定义正则表达式? I tried [a-zA-Z] followed by\\ and dot in side bracket but is not working. 我尝试了[a-zA-Z],后跟\\,并在方括号中加了点,但没有用。 I also tried [ABCD] just for testing but it shows invalid text for ABC. 我还尝试了[ABCD]只是为了进行测试,但是它显示了ABC无效的文本。 What might be the reason???? 可能是什么原因???? Thanks to all... 谢谢大家...

You will need to escape the dot as in regex it normally means 'any character' 您需要像正则表达式中那样转义该点,通常表示“任何字符”

[a-zA-Z\.]+

in c# you will need to deinfe the regex string as follows: 在C#中,您需要按如下所示定义正则表达式字符串:

var r = @"[a-zA-Z\.]+";

Edit based on comment: 根据评论进行编辑:

<asp:TextBox ID="qualificationBox" runat="server" Width="250px"></asp:TextBox>
<asp:RegularExpressionValidator ID="NameRequiredValidator" runat="server" 
 ErrorMessage="Please Enter Name" ControlToValidate="qualificationBox" 
 ValidationExpression="[a-zA-Z\.]+"></asp:RegularExpressionValidator>

您可能需要加倍转义反斜杠:

[a-zA-Z\\.]+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM