I have created a function in javascript to check empty textbox. After checking, it should not do further sever side processing if validation fails. It is working fine in IE but not in Firefox. My code is as below. Here what should I write for "window.event.preventDefault();"
to run it in Firefox.[:)]
<%@
Page Language="C#" %>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
I use this function in my products to stop the event and prevent "bubble up" on all types of browsers:
// Prevents event bubble up or any usage after this is called.
// pE - event object
function StopEvent(pE)
{
if (!pE)
if (window.event)
pE = window.event;
else
return;
if (pE.cancelBubble != null)
pE.cancelBubble = true;
if (pE.stopPropagation)
pE.stopPropagation();
if (pE.preventDefault)
pE.preventDefault();
if (window.event)
pE.returnValue = false;
if (pE.cancel != null)
pE.cancel = true;
} // StopEvent
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
Sorry Amit for my previous comment. I tried your code again, it is working. Earlier I did not notice "javascript:return checkForm();" line as you put "return" in it. Now it is fine. Thanks a lot.
Thanks PLBlum for your code. It worked exactly what I wanted. For the sake of other memers who may face the same problem, I am writing the way of calling above function as -
call javascript function on server side code as - Button1.Attributes.Add("onclick","checkForm(event);");
np2316
Member
11 Points
13 Posts
[:)] What is equivalent of 'event.returnValue=false' in Firefox
Feb 06, 2007 06:13 AM|LINK
[:)]Hello,
I have created a function in javascript to check empty textbox. After checking, it should not do further sever side processing if validation fails. It is working fine in IE but not in Firefox. My code is as below. Here what should I write for "window.event.preventDefault();" to run it in Firefox.[:)]
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack){
Button1.Attributes.Add(
"onclick", "javascript:checkForm();");}
}
protected void Button1_Click(object sender, EventArgs e){
LabelError.Text = System.
DateTime.Now.ToString();}
</
script><
html xmlns="http://www.w3.org/1999/xhtml" ><
head runat="server"> <title>Untitled Page</title> <script type="text/javascript"> function checkForm(){
if(document.getElementById('txtUserName').value ==""){
if (window.event) //IE{
alert(
"1");window.
event.returnValue = false;}
else //Firefox{
alert(
"2");window.
event.preventDefault(); // <-- What should I write here instead}
}
}
</script></
head><
body> <form id="form1" runat="server"> <div align="center"> <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br/> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br /> <asp:Label ID="LabelError" runat="server"></asp:Label> </div> </form></
body></
html>javascript in asp.net
KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: [:)] What is equivalent of 'event.returnValue=false' in Firefox
Feb 06, 2007 08:28 AM|LINK
Try this:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button1.Attributes.Add("onclick", "javascript: return checkForm()");
}
}
</script>
<script type="text/javascript">
function checkForm()
{
if(document.getElementById('txtUserName').value =="")
{
return false;
}
return true
}
</script>
Every other will be the same. Mark this as answer if it works.
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
A1ien51
All-Star
29935 Points
5821 Posts
Re: [:)] What is equivalent of 'event.returnValue=false' in Firefox
Feb 06, 2007 11:45 AM|LINK
There is no need for javascript: in the onclick
Eric
np2316
Member
11 Points
13 Posts
Re: [:)] What is equivalent of 'event.returnValue=false' in Firefox
Feb 06, 2007 01:59 PM|LINK
I tried both suggestions as "return false" and without writing "javascript" in "onclick" but did not work.
Please copy the given code and put into .aspx file. It may give you better idea.
Any help is appreciated.
Thanks
Nitin
KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: [:)] What is equivalent of 'event.returnValue=false' in Firefox
Feb 07, 2007 09:06 AM|LINK
Although javascript is not required but i put it in the button onClick handler. Not Sure why you are not been able to make it work.
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
PLBlum
All-Star
30409 Points
5347 Posts
MVP
Re: [:)] What is equivalent of 'event.returnValue=false' in Firefox
Feb 07, 2007 06:50 PM|LINK
I use this function in my products to stop the event and prevent "bubble up" on all types of browsers:
// Prevents event bubble up or any usage after this is called. // pE - event object function StopEvent(pE) { if (!pE) if (window.event) pE = window.event; else return; if (pE.cancelBubble != null) pE.cancelBubble = true; if (pE.stopPropagation) pE.stopPropagation(); if (pE.preventDefault) pE.preventDefault(); if (window.event) pE.returnValue = false; if (pE.cancel != null) pE.cancel = true; } // StopEventCreator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
np2316
Member
11 Points
13 Posts
Re: [:)] What is equivalent of 'event.returnValue=false' in Firefox
Feb 08, 2007 07:04 AM|LINK
Thank you all
Sorry Amit for my previous comment. I tried your code again, it is working. Earlier I did not notice "javascript:return checkForm();" line as you put "return" in it. Now it is fine. Thanks a lot.
Thanks PLBlum for your code. It worked exactly what I wanted. For the sake of other memers who may face the same problem, I am writing the way of calling above function as -
call javascript function on server side code as - Button1.Attributes.Add("onclick","checkForm(event);");
Again change the "checkForm" function as -
function
checkForm(e){
if(document.getElementById('txtUserName').value =="") StopEvent(e);
}
One more solution I got is as under:
call javascript function on server side code as - Button1.Attributes.Add("onclick","checkForm(event);");
And rewrite "checkForm" function as -
function
checkForm(e){
if(document.getElementById('txtUserName').value =="")
{
if (window.event) //IE
window.event.returnValue = false;
else //Firefox
e.preventDefault();
}
}
Cheers,
Nitin