javascript to check textbox is empty

Last post 05-12-2008 4:00 AM by sivakl_2001. 9 replies.

Sort Posts:

  • javascript to check textbox is empty

    05-10-2008, 8:58 AM

    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
  • 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,238

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

    Hope it helps!
    AzamSharp

    RefactorCode
    GridViewGuy
    Interested in writing for GridViewGuy! Email me!
  • Re: javascript to check textbox is empty

    05-12-2008, 12:50 AM

     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 161


    Check out this link on how to validate controls..

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

  • Re: javascript to check textbox is empty

    05-12-2008, 1:22 AM
    Answer

     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

    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

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

     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..

     

  • Re: javascript to check textbox is empty

    05-12-2008, 4:00 AM

    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>

     

Page 1 of 1 (10 items)