asp.net必填字段验证器

时间:2011-09-26 05:41:12

标签: asp.net

只有在单击提交按钮

后才会触发必填字段验证器
<asp:RequiredFieldValidator id="req" controltovalidate="txtphone" errormessage="please enter the details">
</asp:RequiredFieldValidator>

2 个答案:

答案 0 :(得分:1)

如果您使用的是按钮字段,请尝试在验证器和按钮中应用Validation Group属性,如下所示

<asp:RequiredFieldValidator id="req" runate="Server" controltovalidate="txtphone" errormessage="please enter the details" ValidationGroup="g">

<asp:button id="btn" runat="server" validationgroup="g">

答案 1 :(得分:1)

你的代码上缺少runat服务器

检查一下:

<form id="form1" runat="server">
    Phone Number:<br />
    <asp:TextBox runat="server" id="txtphone" />
    <asp:RequiredFieldValidator runat="server" id="req" controltovalidate="txtphone" errormessage="Please enter your phone number!" />
    <br /><br />
    <asp:Button runat="server" id="btnSubmitForm" text="Ok" />
</form>

此致