0

I have an email validation code on client side. It works fine/as expected in IE but somehow doesnot show error messages in Firefox.

Below is the code:

<asp:ImageButton ID="btnLink" runat="server" AlternateText="ClickHere" OnClientClick="return onCClick();" OnClick="btnLink_Click"/>  
<div id="errorEmail" runat="server"></div>

//function to validate
        function onCClick() {
//clear error message
            document.getElementById('<%=errorEmail.ClientID%>').innerText = "";
//if validation fails
            if (validateEmail() != true) {
//show error message
                document.getElementById('<%=errorEmail.ClientID%>').innerText = "Invalid Email Address.";
                return false;
            }
}

function validateEmail() {
            var emailId = document.getElementById('<%=txtEmail.ClientID%>').value;
            var emailPattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
            return emailPattern.test(emailId);
        }

Is there something which i should have taken care of ? My error message div is set to blank but not invisible anywhere(in that case javascript also would not have worked)

BFry
  • 905
  • 2
  • 11
  • 24
  • 1
    possible duplicate of ['innerText' works in IE, but not in Firefox](http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox) – epascarello Jan 28 '13 at 16:11
  • I don't see any reference to the errorEmailAddress `div`. Is it relevant to the question? – Michael Richardson Jan 28 '13 at 16:35
  • it's recommended to use `jQuery` as it's cross browsers – Mahmoud Farahat Jan 28 '13 at 16:36
  • @MahmoudFarahat: May be yes, but will have to change whole code :( and all other validations as well. That too as of now I am not sure if that will work. – BFry Jan 28 '13 at 16:48
  • @epascarello : Thanks for pointer. Never knew if exactly the issue was with innerText or my way of calling or my function. Thanks for pointer, will try out suggestions from that solution. – BFry Jan 28 '13 at 16:52
  • 1
    Just a reminder, any validation you're doing on the client side should also be done on the server side. [Never trust the client](http://stackoverflow.com/questions/3483514/why-is-client-side-validation-not-enough). – Michael Richardson Jan 29 '13 at 00:34
  • Thanks @probackpacker - Good point. – BFry Jan 29 '13 at 09:42

2 Answers2

1

innerText is not cross browser, firefox uses textContent

You can use a function like this:

function changeText(elem, changeVal) {
    if ((elem.textContent) && (typeof (elem.textContent) != "undefined")) {
        elem.textContent = changeVal;
    } else {
        elem.innerText = changeVal;
    }
}

or just use innerHTML.

epascarello
  • 204,599
  • 20
  • 195
  • 236
0

Better you try innerHTML.. it will work..

document.getElementById('errorEmailAddress').innerHTML = "Error Message";
Pandian
  • 8,848
  • 2
  • 23
  • 33
  • throws exception: Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object – BFry Jan 28 '13 at 16:41