Page view counter

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
    • Loading...
    • gauravsuresh
    • Joined on 03-06-2008, 11:24 AM
    • Posts 45
    • Points 26

    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
    • Loading...
    • azamsharp
    • Joined on 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,475
    • Points 23,388

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

    HighOnCoding
    Wanna get high!
  • Re: javascript to check textbox is empty

    05-12-2008, 12:50 AM
    • Loading...
    • gauravsuresh
    • Joined on 03-06-2008, 11:24 AM
    • Posts 45
    • Points 26

     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
    • Loading...
    • jagan.unique
    • Joined on 02-08-2008, 9:42 AM
    • Hyderabad, India
    • Posts 237
    • Points 1,250


    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
    • Loading...
    • gauravsuresh
    • Joined on 03-06-2008, 11:24 AM
    • Posts 45
    • Points 26

     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
    • Loading...
    • sivakl_2001
    • Joined on 01-11-2008, 8:13 AM
    • Kuala Lumpur, Phileo Damansara
    • Posts 769
    • Points 2,773

    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
    • Loading...
    • gauravsuresh
    • Joined on 03-06-2008, 11:24 AM
    • Posts 45
    • Points 26

     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
    • Loading...
    • jagan.unique
    • Joined on 02-08-2008, 9:42 AM
    • Hyderabad, India
    • Posts 237
    • Points 1,250

     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
    • Loading...
    • sivakl_2001
    • Joined on 01-11-2008, 8:13 AM
    • Kuala Lumpur, Phileo Damansara
    • Posts 769
    • Points 2,773

    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
    • Loading...
    • ams16
    • Joined on 05-12-2008, 9:48 AM
    • Posts 77
    • Points 105

    <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
    • Loading...
    • rakesh.khuntia
    • Joined on 04-20-2009, 2:42 PM
    • Posts 3
    • Points 16

    Thank U...

Page 1 of 1 (12 items)