C# 你如何在asp.net中验证电话号码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18585613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you validate phone number in asp.net?
提问by user2240189
I need to validate phone number that allows numeric and symbols + - () how can i do so in asp.net?
我需要验证允许数字和符号 + - () 的电话号码,我如何在 asp.net 中这样做?
<asp:TextBox ID="TxtNo" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Enter valid Phone number" ControlToValidate="TxtNo" ></asp:RegularExpressionValidator>
回答by Ehsan
you need to add
你需要添加
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Enter valid Phone number" ControlToValidate="TxtNo" ValidationExpression="^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$" ></asp:RegularExpressionValidator>
with the expression that should specify the format that needs to be validated. You can choose one from hereand specify it in the validation expression above
使用应指定需要验证的格式的表达式。您可以从这里选择一个并在上面的验证表达式中指定它
回答by Haidar Abbas
ValidationExpression= "^([0-9\(\)\/\+ \-]*)$"
<asp:TextBox ID="TxtNo" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
runat="server" ErrorMessage="Enter valid Phone number"
ControlToValidate="TxtNo"
ValidationExpression= "^([0-9\(\)\/\+ \-]*)$"></asp:RegularExpressionValidator>
回答by Arun Bertil
Try this validation expression
试试这个验证表达式
^([\(]{1}[0-9]{3}[\)]{1}[\.| |\-]{0,1}|^[0-9]{3}[\.|\-| ]?)?[0-9]{3}(\.|\-| )?[0-9]{4}$