开发者

Asp.Net: Client side validation for mandatory field

开发者 https://www.devze.com 2023-04-10 18:19 出处:网络
I have a textbox control <div id=\"divFirstName\" runat=\"server\"> <label>First Name:<span class=\"mandatory\">*</span></label>

I have a textbox control

 <div id="divFirstName" runat="server">
            <label>First Name:<span class="mandatory">*</span></label>
            <asp:TextBox ID="txt_FirstName" runat="server" TabIndex="1" CssClass="text" onkeypress="clearMessage()" ></asp:TextBox>
 </div>

I am applying required field validator for the above control,

<asp:RequiredFieldValidator runat="server" ID="reqFirstName" SetFocusOnError="true" ControlToVal开发者_运维技巧idate="txt_FirstName" Display="None" ErrorMessage="First Name is required." />

Now in a particular sequence I need to disable the validator applied for the text control. How can i achieve this?


You need to disable the validator control. From server side, you can use Enabled property. For example,

reqFirstName.Enabled = false;

In case, you want to disable the validator on client side w/o taking the post-back then you should use client side API for validators. For example,

ValidatorEnable(document.getElementById('<%=reqFirstName.ClientID%>'), false);

Note that even if you disable the validator on the client side, it would be still enable on the server and will trigger validation on the post-back. So you have to handle this situation on server side by probably disabling the validator early in the page life cycle.

For more information on client side API for validators, see this: http://www.aspdotnetfaq.com/Faq/How-to-control-ASP-NET-Validator-Controls-Client-Side-validation-from-JavaScript.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号