javascript to check textbox is empty

Last post 06-04-2009 2:04 AM by rakesh.khuntia. 11 replies.

Sort Posts:

  • javascript to check textbox is empty

    05-10-2008, 8:58 AM
    • Member
      26 point Member
    • gauravsuresh
    • Member since 03-06-2008, 11:24 AM
    • Posts 45

    i am using content page...

    i have a control asp:textbox id=myname

    i want to write the client script for this field to check against being null

    how do i access this control through javascript? 

  • Re: javascript to check textbox is empty

    05-10-2008, 9:17 AM
    Answer

     

    <asp:TextBox id="txtMyTextBox" runat="Server" Text="1234"></asp:TextBox>

    In javascript do the following to get value:

    var value = document.getElementById('<%=txtMyTextBox.ClientID%>').value;

    As TextBox is server control, so on runtime this control have different client id

    '<%=txtMyTextBox.ClientID%>' will return control clientid on runtime

    Hope it helps.

    -Manas

    =======================================
    If this post is useful to you, please mark it as answer.
  • Re: javascript to check textbox is empty

    05-10-2008, 1:42 PM
    Answer
    • All-Star
      23,569 point All-Star
    • azamsharp
    • Member since 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,513

     Is there any reason that you are not using ASP.NET RequiredFieldValidator control?

    HighOnCoding
    Get high on ASP.NET articles, videos, podcasts and more!
  • Re: javascript to check textbox is empty

    05-12-2008, 12:50 AM
    • Member
      26 point Member
    • gauravsuresh
    • Member since 03-06-2008, 11:24 AM
    • Posts 45

     i couldnt find a property like required field validator on my buttons property...could you just guide me with this...there is another group as validation group...what is the input type that it expects?say is it the id of my text box that i need to fill in and if i do that would it validate it against it being null specifiaclly when that button is clicked?

  • Re: javascript to check textbox is empty

    05-12-2008, 1:17 AM
    • Participant
      1,250 point Participant
    • jagan.unique
    • Member since 02-08-2008, 9:42 AM
    • Hyderabad, India
    • Posts 237


    Check out this link on how to validate controls..

     http://asp.net/learn/videos/video-193.aspx 

    Click “Mark as Answer” on the post(s) that helped you.
  • Re: javascript to check textbox is empty

    05-12-2008, 1:22 AM
    Answer
    • Member
      26 point Member
    • gauravsuresh
    • Member since 03-06-2008, 11:24 AM
    • Posts 45

     hey bro,

    i tried doing this onto my button's OnClientClick  var value = document.getElementById('<%=txtName.ClientID%>').value;alert(value);return false;

    but this doesnt return anything rather goes straight into my server code (.cs) ..so that means some error with javascript right?

  • Re: javascript to check textbox is empty

    05-12-2008, 2:19 AM
    Answer
    • Contributor
      2,775 point Contributor
    • sivakl_2001
    • Member since 01-11-2008, 8:13 AM
    • Kuala Lumpur, Phileo Damansara
    • Posts 783

    hi gaurav i try ur coding its working pls check r u try like this 

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="second.aspx.cs" Inherits="filestream_second" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    <script language="javascript">

     

    function check()

    {

    var value = document.getElementById('<%=TextBox3.ClientID%>').value;

    alert(value);

    return false;

    // if(value=="")

    // {

    // alert("empty");

    //

    // }

    // else

    // {

    // alert(value);

    // }

    }

    </script>

    </head>

    <body>

    <form style="" id="form1" runat="server">

    <div>

    <asp:Button ID="Button6" OnClientClick="check()" runat="server" OnClick="Button6_Click" Text="Button" />

    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

    <br />

    <br />

    </form>

     

    </body>

    </html>

  • Re: javascript to check textbox is empty

    05-12-2008, 2:54 AM
    • Member
      26 point Member
    • gauravsuresh
    • Member since 03-06-2008, 11:24 AM
    • Posts 45

     hey i think it is all bcoz of me using content page...this page is a content page...is it possible to write scripts in content page( it doesnt have a head part, only body part)..

  • Re: javascript to check textbox is empty

    05-12-2008, 3:47 AM
    Answer
    • Participant
      1,250 point Participant
    • jagan.unique
    • Member since 02-08-2008, 9:42 AM
    • Hyderabad, India
    • Posts 237

     Hi

    If you are using master page then the server side control id will be changed.

    use document.getElementById("ctl00$ContentPlaceHolder1$TextBox1").value==""  to validate the control..

    if problem not solved then Check the content page control id by viewing the  source & copy the controlid & paste it into getElementById() method.

     
    Here is a sample code..

    MasterPage:

    <head>
    <script type="text/javascript">
    function test()
    {
          
           if (document.getElementById("ctl00$ContentPlaceHolder1$TextBox1").value==""){
           alert("Please enter the text");
           return false;
           }
           else
           {
           return true;
           }
    }
    </script>
    </head>

     

    Child page:

     

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return test();" /></td>
     </asp:Content>

     

    Note: Instead of writing the whole code to validate a control,you can use required field validator controls by just setting the few properties..

     

    Click “Mark as Answer” on the post(s) that helped you.
  • Re: javascript to check textbox is empty

    05-12-2008, 4:00 AM
    Answer
    • Contributor
      2,775 point Contributor
    • sivakl_2001
    • Member since 01-11-2008, 8:13 AM
    • Kuala Lumpur, Phileo Damansara
    • Posts 783

    hi gaurav u can write javascript function in contenpage

    <%@ Page Language="C#" MasterPageFile="~/myMaster.master" AutoEventWireup="true" CodeFile="newService.aspx.cs" Inherits="newService" Title="Untitled Page" %>

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc2" %>

     

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <script language="javascript" >

    function printPage()

    {

     

     

    }

     

     

    function get_Data()

    {

    }

     

    }

    </script>

     

     

    <table align="center" style="vertical-align:top " width="100%" ><tr><td align="left" style="height: 153px; width: 50%;" >

    <table>

    </asp:Content>

     

  • Re: javascript to check textbox is empty

    05-16-2008, 7:35 AM
    Answer
    • Member
      105 point Member
    • ams16
    • Member since 05-12-2008, 9:48 AM
    • Posts 77

    <script language="javascript">

    if (document.getElementById("myname").value.length == 0)

    {

    alert("TextBox is empty");

    }

     

    </script>

  • Re: javascript to check textbox is empty

    06-04-2009, 2:04 AM

    Thank U...

Page 1 of 1 (12 items)