Click here to Skip to main content
15,898,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi to all,
Here i using this below script to validate mobile and email but it work in all browser expect firefox got error "
window.event is undefined
"


Script:-
C#
function CheckMode() {
    var ctr = document.getElementById('ctl00_cplhControlPanel_frmViewAdd_tablInsert_tabInsMain_drpModeofContactAdd');
    var ctrMobile = document.getElementById('ctl00_cplhControlPanel_frmViewAdd_tablInsert_tabInsMain_txtMobileAdd');
    var ctrEmail = document.getElementById('ctl00_cplhControlPanel_frmViewAdd_tablInsert_tabInsMain_txtEmailIdAdd');
    var txt = ctr.value;
    var Mobile = ctrMobile.value;
    var Email = ctrEmail.value;

    if (txt == "1") {
        if (Mobile == "") {
            alert("Enter Mobile Number");
            Page_IsValid = false;
            return window.event.returnValue = false;
        }
    }
    else if (txt == "2") {
        if (Email == "") {
            alert("Enter Email Id");
            Page_IsValid = false;
            return window.event.returnValue = false;
        }
    }
    else {
        Page_IsValid = true;
    }
}



button:-
XML
<asp:Button ID="InsertButton" TabIndex="17" CssClass="savebutton1231" EnableTheming="false"
                                                                                    runat="server" CausesValidation="True" CommandName="Insert" SkinID="skinBtnSave"
                                                                                    OnClientClick="CheckMode();" OnClick="InsertButton_Click"></asp:Button>


Kindly help me for this task i need to work in all browser.Thanks in advance.
Posted

window.event does not exist in Firefox. Try adding a parameter e to your CheckMode function, then change OnClientClick to CheckMode(event);. That will pass the event to CheckMode.

You can also add a line e = e || window.event; to your function that gives e the value of window.event, in case the parameter e passed to CheckMode is undefined but window.event is not.
JavaScript
function CheckMode(e) {
    e = e || window.event;
    ...

ASP.NET
<asp:Button ID="InsertButton" TabIndex="17" CssClass="savebutton1231" EnableTheming="false" runat="server" CausesValidation="True" CommandName="Insert" SkinID="skinBtnSave" OnClientClick="CheckMode(event);" OnClick="InsertButton_Click"></asp:Button>
 
Share this answer
 
v2
Comments
JOTHI KUMAR Member 10918227 14-Jul-15 3:11am    
i done this but this will not work me i got event is undefined.this is form view
Thomas Daniels 14-Jul-15 3:13am    
Where did you get that? At what line?
Sergey Alexandrovich Kryukov 14-Jul-15 3:13am    
No, you haven't done it properly!
—SA
Thomas Daniels 14-Jul-15 3:14am    
I updated my solution by the way, "this" didn't appear to work in Firefox while "event" did. You might want to try again.
Sergey Alexandrovich Kryukov 14-Jul-15 3:14am    
5ed. See also Solution 2 for information on some a bit different aspects.
—SA
You should never use this property; it does not exist in standard. Please see: https://developer.mozilla.org/en-US/docs/Web/API/Window[^].

This property probably only exist in IE, and it reflects the big conceptual technology abuse: passing the event information through outer context, so this object is only defined when the event is invoked. The whole idea to use such unreliable technique is badly wrong, but who tells that anything implemented in IE can be trusted?

The correct approach is to pass event object as the first argument of a handler. This is the simplest example:
HTML
<button onclick="myHandler(event)">Some Button</button> <!-- use this exact name, "event" -->
In JavaScript, you may have
C#
function myHandler(eventInstance) {
   // use eventInstance here
}

With click, you don't have any essential information, but with mouse or keyboard events you can get mouse coordinates, or key data, and so on. Please see: https://developer.mozilla.org/en-US/docs/Web/API/Event[^].

Try to use correct standard techniques, never buy IE provocations. :-)

—SA
 
Share this answer
 
v3
Comments
Thomas Daniels 14-Jul-15 3:15am    
5ed.
Sergey Alexandrovich Kryukov 14-Jul-15 3:16am    
Thank you.
—SA
JOTHI KUMAR Member 10918227 14-Jul-15 8:34am    
please suggest any answer derived from given my code.
Sergey Alexandrovich Kryukov 14-Jul-15 10:32am    
I explained exactly what to do, please deal with you code yourself. Deriving anything from buggy code makes no sense.

If you need more help, you can re-write your code using out directions and show appropriate sample, without the bug you demonstrated, if something is not working. There is no need to look at buggy code, after I explained to you where the bug is. The typical reasonable practice of code review is to look at it up to the first big problem.

I pointed out yours; now it's your turn.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900