0
 function isGoodEmail() {
            var email = document.getElementById("<%=txtComapnyEmail.ClientID%>").value;
            if (email == "Email") {
                if (window.isValidEmail(email)) {
                    if (/(aol|gmail|yahoo|hotmail)\.com$/.test(email)) {
                        alert(' valid email, but not for this site.  No free service emails!');
                        return false;
                    }
                    return true;
                }
                return false;
            }

Button code:

<asp:Button runat="server" class="button-orange" Text="Confirm and start my company page"
                                ID="Companystart" OnClick="CompanystartClick" OnClientClick="isGoodEmail" />

I am calling the above JavaScript from the button, but it is not validating the email. Even if entered gmail it is accepted.

mbinette
  • 5,094
  • 3
  • 24
  • 32
Manoj Chowdary
  • 97
  • 1
  • 3
  • 15
  • Based on your post, its "firing" exactly as you wrote it. Why would you think it would do anything if you typed `gmail` with `if(email=="Email")`? What is `window.isValidEmail`? – EdSF Nov 22 '12 at 06:24

2 Answers2

0

use the following

 <asp:Button runat="server" class="button-orange" Text="Confirm and start my company page"
                            ID="Companystart" OnClick="CompanystartClick" OnClientClick="return isGoodEmail()" />

Your are missing 'return' before your client click function call OnClientClick="return isGoodEmail()"

As you have written return true and false in your java-script so in your function call too there should be return before the function name.

For better understanding of return true and false you can go through this link

What's the effect of adding 'return false' to a click event listener?

---EDIT

 function isGoodEmail() {
        alert("In the beginning");
        var email = document.getElementById("<%=txtComapnyEmail.ClientID%>").value;
        alert(email);
        if (email == "Email") {

            if (window.isValidEmail(email)) {
                if (/(aol|gmail|yahoo|hotmail)\.com$/.test(email)) {
                    alert(' valid email, but not for this site.  No free service emails!');
                    return false;
                }
                return true;
            }
            return false;
        }
Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
0

If you put OnClientClick="return isGoodEmail()" it will work it invokes JavaScript code.

but suppose you put "gmail" as text for email, then the if condition

if (email == "Email")

is false so nothing is executed inside or returned hence it feels like JavaScript is not invoked

try using firebug to debug and break-point js code

RohitWagh
  • 1,999
  • 3
  • 22
  • 43
  • function isGoodEmail() { var email = document.getElementById("<%=txtComapnyEmail.ClientID%>").value; alert('Script entered');} I tried in the following to whether is firing script or not.It has not been working – Manoj Chowdary Nov 22 '12 at 06:34