0

I don't understand why I am getting alert "Empty" when I type some numbers or leave a empty? Can you help me?

My Code:

    </script> 

               <script type="text/javascript">
                   function tell() {
                       var v = true;
                       if (document.getElementsByName("TextBox33").length == 0)
                       alert("Empty");
                           v = false;

                       return v;
                   }
</script>

<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert" OnClientClick="javaScript: return tell();"

<asp:TextBox ID="TextBox33" onkeypress="return isNumberKey(event)" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>

I am adding more html:

<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Insert" AutoGenerateRows="False" DataKeyNames="id" DataSourceID="SqlDataSource1" Height="50px" Width="286px" OnItemInserting="DetailsView1_ItemInserting" OnDataBound="DetailsView1_DataBound" OnItemInserted="DetailsView1_ItemInserted">

        <asp:TemplateField HeaderText="IMEI" SortExpression="imei">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox33" onkeypress="return isNumberKey(event)" CssClass="TextBox33" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Bind("imei") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
1
  • GetElementsByName returns a collection. It's empty not the TextBox. Commented Mar 21, 2015 at 14:25

1 Answer 1

2

You're calling getElementsByName which returns al array of DOM elements. You're then taking the length of that, which will always be more than 0 because the element exists.

Try adding a [0] after the getElementsByName call, and then change .text to .value.

Also so you're aware, you need to put the alert and v = false inside brackets. You can only skip brackets if you're executing one statement.

Here's the revised code:

if (document.getElementById("TextBox33").value.length == 0){
    alert("Oh no I'm empty!");
    v = false;
}

Update: I also just noticed that you didn't actually give the textbox a name of TextBox33, you gave it an ID. I've updated the code.

Sign up to request clarification or add additional context in comments.

10 Comments

Sure, I added it to the answer.
I have got error: 0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference
My bad, I'm typing on a phone. I had ID in all caps, it's Id. Try now.
Yes, Luke, I have tried with ID, but the problem is same and I don't know why... 0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference
Yes. document.getElementById("TextBox33").value.length == 0
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.