简体   繁体   English

JavaScript无法在ASP.NET中触发

[英]JavaScript not firing in ASP.NET

 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. 我正在通过按钮调用上面的JavaScript,但是它没有验证电子邮件。 Even if entered gmail it is accepted. 即使输入gmail ,也会被接受。

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()" 在客户端单击功能调用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. 由于您已经在Java脚本中编写了return true和false,因此在函数调用中,在函数名之前也应该包含return。

For better understanding of return true and false you can go through this link 为了更好地了解return true and false您可以通过此链接

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;
        }

If you put OnClientClick="return isGoodEmail()" it will work it invokes JavaScript code. 如果您将OnClientClick="return isGoodEmail()"它将调用JavaScript代码。

but suppose you put "gmail" as text for email, then the if condition 但假设您将“ gmail”作为电子邮件的文本,则为if条件

if (email == "Email")

is false so nothing is executed inside or returned hence it feels like JavaScript is not invoked 为false,因此没有在其中执行任何内容或未返回任何内容,因此感觉好像未调用JavaScript

try using firebug to debug and break-point js code 尝试使用Firebug调试和断点js代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM