我希望验证表达式为必需的字段验证器和数字验证器一起用于电话号码

时间:2013-03-22 11:08:57

标签: asp.net validation

我想要所需的字段验证器和数字验证器的验证表达式 电话号码

  <asp:TextBox ID="txtPhonenumber" runat="server" MaxLength="11" TabIndex="11" onKeyPress="return validateNumbersOnly();" ></asp:TextBox>

            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Enter Phone Number" ControlToValidate="txtPhonenumber" ></asp:RequiredFieldValidator>

此代码仅在字段为空时才有效

2 个答案:

答案 0 :(得分:2)

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPhonenumber" ErrorMessage="Only Numbers" Display="None" ValidationExpression="^\d+$" ></asp:RegularExpressionValidator>

ValidationExpression = “^ \ d + $” 用它来进行数字格式验证

答案 1 :(得分:0)

<asp:TextBox ID="txtPhonenumber" runat="server" MaxLength="11"  ></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter Phone Number" ControlToValidate="txtPhonenumber" ></asp:RequiredFieldValidator><asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPhonenumber" ErrorMessage="Only Numbers" Display="None" ValidationExpression="^\d+$" ></asp:RegularExpressionValidator>