验证三个文本框,包括电话号码
如何在 C# 中添加 ASP.NET 验证,所有三个文本框都不为空,并且电话号码的格式为 DDDD-DDDD,其中 D 为 0 – 9?
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPhoneNumber" runat="server"></asp:TextBox>
How do I add ASP.NET validation in C# all three text boxes aren't empty and the phone number has the format of DDDD-DDDD where D is 0 – 9?
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPhoneNumber" runat="server"></asp:TextBox>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过自己解决这个问题?该文档很容易找到:
http://msdn.microsoft.com/en -us/library/aa479013.aspx
您需要使用必填字段验证器 - 像这样:
您还需要一个正则表达式验证器,例如:
注意 reg 表达式的工具箱有许多内置的,包括大多数电话号码格式。我只会使用其中之一。
Have you tried to solve this problem yourself? The documentation is easy to find:
http://msdn.microsoft.com/en-us/library/aa479013.aspx
You need to use a required field validator -- something like this:
You will also want a regular expression validator, something like:
NB The toolbox for reg expression has many built in, including most phone number formats. I would just use one of those.
使用 RequiredFieldValidator 和 ReguralExpressionValidator 控件。
RequiredFieldValidator 服务器控件确保用户输入内容进入表单中与其关联的字段。您需要将 RequiredFieldValidator 绑定到每个控件表单中的必填字段。
RegularExpressionValidator 服务器控件是一个验证控件,使您能够根据正则表达式定义的模式检查用户的输入。
请参阅:
验证 ASP.NET 服务器控件
Use RequiredFieldValidator and ReguralExpressionValidator controls.
The RequiredFieldValidator server control makes sure that the user enters something into the field that it is associated with in the form. You need to tie the RequiredFieldValidator to each control that is a required field in the form.
The RegularExpressionValidator server control is a validation control that enables you to check the user's input based on a pattern defined by a regular expression.
Refer to:
Validating ASP.NET Server Controls
标记:
正则表达式 我猜应该是:
或者(如果你不想创建变量位数,例如 2 到 4):
含义:
Markup:
The RegEx should be I guess:
or (if you don't want to make a variable number of digits, e.g. 2 to 4):
what means: