验证三个文本框,包括电话号码

发布于 2024-11-07 17:12:03 字数 321 浏览 1 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一场信仰旅途 2024-11-14 17:12:03

您是否尝试过自己解决这个问题?该文档很容易找到:

http://msdn.microsoft.com/en -us/library/aa479013.aspx

您需要使用必填字段验证器 - 像这样:

 <asp:RequiredFieldValidator 
         id="RequiredFieldValidator1" runat="server" 
         ErrorMessage="Required!" 
         ControlToValidate="txtFirstName">

您还需要一个正则表达式验证器,例如:

<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
   runat="server" ControlToValidate="txtPhoneNumber" 
    ErrorMessage="wrong!" 
    ValidationExpression="^\d\d\d\d-\d\d\d\d$">
</asp:RegularExpressionValidator>

注意 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:

 <asp:RequiredFieldValidator 
         id="RequiredFieldValidator1" runat="server" 
         ErrorMessage="Required!" 
         ControlToValidate="txtFirstName">

You will also want a regular expression validator, something like:

<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
   runat="server" ControlToValidate="txtPhoneNumber" 
    ErrorMessage="wrong!" 
    ValidationExpression="^\d\d\d\d-\d\d\d\d$">
</asp:RegularExpressionValidator>

NB The toolbox for reg expression has many built in, including most phone number formats. I would just use one of those.

editor

扶醉桌前 2024-11-14 17:12:03

使用 RequiredFieldValidatorReguralExpressionValidator 控件。

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

山田美奈子 2024-11-14 17:12:03

标记:

<table>
<tr>
<td><asp:TextBox ID="txtFirstName" runat="server" /></td>
<td><asp:RequiredFieldValidator runat="server" ControlToValidate="txtFirstName" Display="Dynamic" ValidationGroup="group"></asp:RequiredFieldValidator></td>
<td><asp:RegularExpressionValidator runat="server" ControlToValidate="txtFirstName" ValidationExpression="..." Display="Dynamic" ValidationGroup="group">*</asp:RegularExpressionValidator></td>
</tr>
<!-- do the same 2 times more -->
</table>

正则表达式 我猜应该是:

^\d{4,4}-\d{4,4}$

或者(如果你不想创建变量位数,例如 2 到 4):

^\d\d\d\d-\d\d\d\d$

含义:

line begin
4 digits, not more or less
dash
4 digits, not more or less
line end

Markup:

<table>
<tr>
<td><asp:TextBox ID="txtFirstName" runat="server" /></td>
<td><asp:RequiredFieldValidator runat="server" ControlToValidate="txtFirstName" Display="Dynamic" ValidationGroup="group"></asp:RequiredFieldValidator></td>
<td><asp:RegularExpressionValidator runat="server" ControlToValidate="txtFirstName" ValidationExpression="..." Display="Dynamic" ValidationGroup="group">*</asp:RegularExpressionValidator></td>
</tr>
<!-- do the same 2 times more -->
</table>

The RegEx should be I guess:

^\d{4,4}-\d{4,4}$

or (if you don't want to make a variable number of digits, e.g. 2 to 4):

^\d\d\d\d-\d\d\d\d$

what means:

line begin
4 digits, not more or less
dash
4 digits, not more or less
line end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文