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?
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)..
gauravsuresh
Member
26 Points
45 Posts
javascript to check textbox is empty
May 10, 2008 12:58 PM|LINK
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?
yeotumitsu@s...
Contributor
4907 Points
836 Posts
Re: javascript to check textbox is empty
May 10, 2008 01:17 PM|LINK
<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
-Manas
=======================================
If this post is useful to you, please mark it as answer.
azamsharp
All-Star
24614 Points
4612 Posts
Re: javascript to check textbox is empty
May 10, 2008 05:42 PM|LINK
Is there any reason that you are not using ASP.NET RequiredFieldValidator control?
HighOnCoding
gauravsuresh
Member
26 Points
45 Posts
Re: javascript to check textbox is empty
May 12, 2008 04:50 AM|LINK
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?
jagan.unique
Participant
1286 Points
245 Posts
Re: javascript to check textbox is empty
May 12, 2008 05:17 AM|LINK
Check out this link on how to validate controls..
http://asp.net/learn/videos/video-193.aspx
gauravsuresh
Member
26 Points
45 Posts
Re: javascript to check textbox is empty
May 12, 2008 05:22 AM|LINK
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?
sivakl_2001
Contributor
2849 Points
1069 Posts
Re: javascript to check textbox is empty
May 12, 2008 06:19 AM|LINK
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>gauravsuresh
Member
26 Points
45 Posts
Re: javascript to check textbox is empty
May 12, 2008 06:54 AM|LINK
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)..
jagan.unique
Participant
1286 Points
245 Posts
Re: javascript to check textbox is empty
May 12, 2008 07:47 AM|LINK
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..
sivakl_2001
Contributor
2849 Points
1069 Posts
Re: javascript to check textbox is empty
May 12, 2008 08:00 AM|LINK
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>