var flgSubmit=true;
var pat_errbox='<%=txt_pat_errbox%>'
var pat_AccountNo =
'<%=txt_pat_AccountNo.ClientID%>';
var val_pat_AccountNo = document.getElementById(pat_AccountNo).value;
var val_errbox=document.getElementById('<%=txt_pat_errbox%>').value;
if(val_pat_AccountNo.trim()=="")
{
//alert("Patient Number Has to be Entered.");
val_errbox=val_errbox +
"Patient Number Has to be Entered."
flgSubmit=false;
function fnValidate()
{
if (document.getElementById('<%=TextBox1.ClientID%>').value=="")
{
document.getElementById('<%=TextBox1.ClientID%>').value ="Patient Number Has to be Entered.";
return false;
}
I tried the way you said, Its still giving me some err: Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object
this is how i tried. Am i missing something still??? FYI: i am using the script tags under <Content> tags, because am using a content page (subpage of a master page).
function fnValidate()
{
var flgSubmit=true;
//var pat_errbox='<%=txt_pat_errbox.ClientID%>';
var pat_AccountNo =
'<%=txt_pat_AccountNo.ClientID%>';
var val_pat_AccountNo = document.getElementById(pat_AccountNo).value;
//var val_errbox=document.getElementById(pat_errbox).value;
if(val_pat_AccountNo.trim()=="")
{
//alert("Patient Number Has to be Entered.");
//val_errbox=val_errbox + "Patient Number Has to be Entered."
document.getElementById(
'<%=txt_pat_errbox.ClientID%>').value="Patient Number Has to be
Entered." // --->>> Error occurs in this line.
flgSubmit=false;
Compiler Error Message: BC30108: 'TextBox' is a type and cannot be used as an expression.
Source Error:
Line 20: //val_errbox=val_errbox + "Patient Number Has to be Entered."
Line 21: //document.getElementById('<%=txt_pat_errbox.ClientID%>').value="Patient Number Has to be Entered." //val_errbox
Line 22: document.getElementById('<%= ((TextBox)Page.Master.FindControl("txt_pat_errbox")).ClientID %>').value="Patient Number Has to be Entered."
Line 23: flgSubmit=false;
Line 24: }
I don't know because it works for me. Are you sure that you have a Master Page? Are you sure that "txt_pat_errbox" is correct?
Try: var textBoxId = '<%= ((TextBox)Page.FindControl("TextBox1")).ClientID %>';
If you still can't fix the error, do a View Source in your browser, look for the TextBox, and post the ID here.
The error occured because the control was visibly false.(in aspx page), hence it was'nt able to find the control and triggered an error.
Now, when i tried the same code with txt_pat_errbox visible="true" i am able to get what i wanted. that is.. when i clicked submit button, the patientno textbox was checked for null value and error text was shown in txt_pat_errbox right away.
mm.. but i actually want the same box with error message , visible, only when there is (atleast 1) error.
i tried this before setting txtbox value:
document.getElementById(
'<%=txt_pat_errbox.ClientID%>').visible="True" ---------------------------->>>>>>>
but again same problem, it could'nt detect this control with visible=false.
document.getElementById(
'<%=txt_pat_errbox.ClientID%>').value =
"Patient Number Has to be Entered.";
//val_errbox
harimena
Member
7 Points
26 Posts
how to set a value of a textbox from javascript function without submitting the page.
Nov 05, 2008 08:23 PM|LINK
I have a simple javascript function in aspx page. I would like to validate a required field.
If the textbox was empty, then set the errormesssage_textbox value to "Patient number is required."
HELP!!!!!
<script type="text/javascript" language="javascript">
function fnshowalert(){
var flgSubmit=true; var pat_errbox='<%=txt_pat_errbox%>' var pat_AccountNo = '<%=txt_pat_AccountNo.ClientID%>'; var val_pat_AccountNo = document.getElementById(pat_AccountNo).value; var val_errbox=document.getElementById('<%=txt_pat_errbox%>').value;
if(val_pat_AccountNo.trim()==""){
//alert("Patient Number Has to be Entered.");val_errbox=val_errbox +
"Patient Number Has to be Entered." flgSubmit=false;}
else{
flgSubmit=true;}
return flgSubmit;}
</script>javascript script javascript validations in asp.net javascript validation javascript server-side client-side page methods webmethod scriptmethod
ameenkpn
Contributor
4805 Points
814 Posts
Re: how to set a value of a textbox from javascript function without submitting the page.
Nov 06, 2008 01:01 AM|LINK
<script>
function fnValidate()
{
if (document.getElementById('<%=TextBox1.ClientID%>').value=="")
{
document.getElementById('<%=TextBox1.ClientID%>').value ="Patient Number Has to be Entered.";
return false;
}
return true;
}
</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return fnValidate();"></asp:Button>
harimena
Member
7 Points
26 Posts
Re: how to set a value of a textbox from javascript function without submitting the page.
Nov 06, 2008 02:30 PM|LINK
Hi Ameenkpn,
I tried the way you said, Its still giving me some err: Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object
this is how i tried. Am i missing something still??? FYI: i am using the script tags under <Content> tags, because am using a content page (subpage of a master page).
function fnValidate(){
var flgSubmit=true; //var pat_errbox='<%=txt_pat_errbox.ClientID%>'; var pat_AccountNo = '<%=txt_pat_AccountNo.ClientID%>'; var val_pat_AccountNo = document.getElementById(pat_AccountNo).value; //var val_errbox=document.getElementById(pat_errbox).value;
if(val_pat_AccountNo.trim()==""){
//alert("Patient Number Has to be Entered."); //val_errbox=val_errbox + "Patient Number Has to be Entered."document.getElementById(
'<%=txt_pat_errbox.ClientID%>').value="Patient Number Has to be Entered." // --->>> Error occurs in this line. flgSubmit=false;}
else{
flgSubmit=true;}
return flgSubmit;}
</script>javascript script javascriptript register IsClientScriptBlockRegistered RegisterClientScriptBlock Javascript client scripting javascript validation
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: how to set a value of a textbox from javascript function without submitting the page.
Nov 06, 2008 05:43 PM|LINK
Try:
var textBoxId = '<%= ((TextBox)Page.Master.FindControl("TextBox1")).ClientID %>';
NC...
harimena
Member
7 Points
26 Posts
Re: how to set a value of a textbox from javascript function without submitting the page.
Nov 06, 2008 05:57 PM|LINK
now its a diff error. What do i do?
Compiler Error Message: BC30108: 'TextBox' is a type and cannot be used as an expression.
Source Error:
document.getElementById javascript validation
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: how to set a value of a textbox from javascript function without submitting the page.
Nov 06, 2008 06:12 PM|LINK
I don't know because it works for me. Are you sure that you have a Master Page? Are you sure that "txt_pat_errbox" is correct?
Try: var textBoxId = '<%= ((TextBox)Page.FindControl("TextBox1")).ClientID %>';
If you still can't fix the error, do a View Source in your browser, look for the TextBox, and post the ID here.
NC...
harimena
Member
7 Points
26 Posts
Re: how to set a value of a textbox from javascript function without submitting the page.
Nov 06, 2008 07:38 PM|LINK
Nc.., here was the interesting part in my code.
The error occured because the control was visibly false.(in aspx page), hence it was'nt able to find the control and triggered an error.
Now, when i tried the same code with txt_pat_errbox visible="true" i am able to get what i wanted. that is.. when i clicked submit button, the patientno textbox was checked for null value and error text was shown in txt_pat_errbox right away.
mm.. but i actually want the same box with error message , visible, only when there is (atleast 1) error.
i tried this before setting txtbox value:
document.getElementById(
'<%=txt_pat_errbox.ClientID%>').visible="True" ---------------------------->>>>>>> but again same problem, it could'nt detect this control with visible=false.document.getElementById(
'<%=txt_pat_errbox.ClientID%>').value = "Patient Number Has to be Entered."; //val_errboxflgSubmit=
false;...
<asp:Panel ID="pnl_pat_errbox" runat="server" Visible="false"> <asp:TextBox ID="txt_pat_errbox" BackColor="Transparent" BorderStyle="None" CssClass="smallredtext" TextMode="MultiLine" Rows="10" runat="server" Height="185" Width="280" Text=""></asp:TextBox> </asp:Panel>WHAT NEXT..........???
thanks NC..you have been giving me wonderful suggestions.
java script validations
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: how to set a value of a textbox from javascript function without submitting the page.
Nov 07, 2008 11:34 AM|LINK
Server-side:
Hide: yourControl.Style.Add("display", "none")
Show: yourControl.Style.Add("display", "")
Client-side:
Hide: document.getElementById('<%= yourControl.ClientID %>').style.display = 'none'
Show: document.getElementById('<%= yourControl.ClientID %>').style.display = ''
You can also use "block" or "inline" to show the element, but they produce different effects, so experiment around with them.
NC...
harimena
Member
7 Points
26 Posts
Re: how to set a value of a textbox from javascript function without submitting the page.
Nov 07, 2008 02:36 PM|LINK
Thanks Very much. I got this worked Now.[:)]