![](/img/trans.png)
[英]Having conflict in using ajax and regular expression validator for the same textbox in asp.net?
[英]How to validate textbox using regular expression validator in ASP.NET
嗨,我想使用正则表达式验证器在ASP.NET中验证我的文本框。
我的文本框应仅包含数字和特殊字符。
我可以使用什么验证表达式?
请帮我。
只有数字可以进入该文本框
我们可以为此使用正则表达式验证器:在验证表达式属性中,保留^ \\ d + $。
<asp:TextBox ID="TextBox1" runat="server"
Style="z-index: 100; left: 259px; position: absolute; top: 283px"
ValidationGroup="check"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Please Enter Only Numbers"
Style="z-index: 101; left: 424px; position: absolute; top: 285px"
ValidationExpression="^\d+$" ValidationGroup="check">
</asp:RegularExpressionValidator>
有效电话号码(例如(999)999-9999)的表达式为:
^[\d\s\(\)\-]+$
这意味着它出现0到9位数,()括号内是减号和空格。 它们都可能发生一次或多次,因此(()3-4 4)也将是正确的。 但是由于您希望spl字符无处不在,所以这是适当的。
也许使用System.ComponentModel.DataAnnotations命名空间中的属性验证。 该属性如下所示:
[RegularExpression(@"^[\d\s\(\)\-]+$")]
public string NumbersAndSpecialChars { get; set; }
使用表达式的Asp控件如下所示(如Rob回答):
<asp:TextBox ID="TextBox1" runat="server"
Style="z-index: 100; left: 259px; position: absolute; top: 283px"
ValidationGroup="check"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="You must enter a phone number in the form of (999) 999-9999."
Style="z-index: 101; left: 424px; position: absolute; top: 285px"
ValidationExpression="^[\d\s\(\)\-]+$" ValidationGroup="check"/>
试试这个模式[-+\\d()]+
。 我认为+
有时也包含在电话号码中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.