简体   繁体   中英

How to write JavaScript validation for a textbox in repeater control?

I have a textbox in repeater control and I have a submit button. I want to validate the textbox using JavaScript such that after entering values in textbox, only one should be submitted. For that, I tried the following. My repeater control is:

  <asp:Repeater Runat="server" ID="repeater" EnableViewState="True">
   <ItemTemplate>
     <asp:textbox id="txtbox" runat="server"/>
   </ItemTemplate>
  </asp:Repeater>

<script>
       function validate()
        {
           if(document.getelementbyid("txtbox").value=="")
            {
              alert("enter value in textbox");
              return false;
            }
         }
</script>

But this is showing error. Error is a JavaScript error ( Object Required ). Please help me. It is a C# web application.

The error you are seeing is because the generated html has no element with an id of "txtbox". There will be several input elements with long complicated ids that might be something like "repeater_txtbox_ctl001".

I'll need more detail about what you are trying to do but you might want to use the ASP.NET validation controls .

ZenMaster is also correct about the case of the call. However, I suspect you will still have the error even if you correct the case.

Switch document.getelementbyid to: document.getElementById regardless of your server-side code.

JavaScript is case sensitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM