使用Onclientclick或Onsubmit不会调用JavaScript函数

时间:2017-06-20 09:05:28

标签: javascript c# html asp.net

我正在尝试将验证应用到我的登录页面。使用单独的JavaScript函数,但不以任何方式调用该函数。

<script type="text/javascript">
    function myFunction() {
        var pass = document.getElementById("passwordsignup").value;
        var ConfirmPass = document.getElementById("passwordsignup_confirm").value;
        var email = document.getElementById("emailsignup").value;
        var uname = document.getElementById("usernamesignup").value;

        var PassExpr = "/^[a-zA-Z0-9!@#$%^&*]{6,16}$/";
        var emailExp = "/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([com\co\.\in])+$/";

        if (uname == "" && email == "" && pass == "" && ConfirmPass == "") {
            alert("All Feilds are Required");

            return false;
        }
        if (uname == "")
        {
            alert("Please Enter User Name !!");
            return false;

        }
        if (email == "")
        {
            alert("Please Enter Email !!")
            return false;
        }
        else {
            if (!email.match(emailExp)) {
                alert("Please Enter Valid Email Id");
                return false;
            }
        }
        if (pass == "")
        {
            alert("Please Enter Password..!!");
            return false;
        }
        else {
            if(pass.length>6 && pass.length<16)
            {
                if (!PassExpr.test(pass)) {
                    alert("password should contain atleast one number and one special character");
                    return false;
                                  }
                }

                else {
                    return false;
                }

            }
            if (ConfirmPass == "")
            {
                alert("Please Re-Enter Password..!!");
                return false;
            }
            else {
                if (pass != ConfirmPass) {
                    alert("Please Re-Enter Password to Match !!!!!");
                    return false;
                }
            }
         }

我的表格是:

 <div id="register" class="animate form">
     <form id="form2" runat="server" onsubmit="return myFunction();">
         <h2> Sign up </h2> 
         <p> 
             <label for="usernamesignup" class="uname">UserName</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
             <input id="usernamesignup" name="usernamesignup" required="required" type="text"  placeholder="User Name" runat ="server" onchange="checkUserName();"/>
             <%--<asp:TextBox ID="usernamesignup1" runat="server" OnTextChanged="checkUserName()" ></asp:TextBox>--%>
             <asp:Label ID="LabelStatus" runat="server" Text="Label"></asp:Label><br />
         </p>
         <p> 
             <label for="emailsignup" class="youmail" >Email&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
             <input id="emailsignup" name="emailsignup" required="required"  type="email" placeholder="Email" runat="server" />

         </p>
         <p> 
             <label for="passwordsignup" class="youpasswd">password&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </label>
             <input id="passwordsignup" name="passwordsignup" required="required" type="password" placeholder="Password" runat="server"/>
         </p>
         <p> 
             <label for="passwordsignup_confirm" class="youpasswd">Please confirm your password&nbsp; </label>
             <input id="passwordsignup_confirm" name="passwordsignup_confirm" required="required" title="Please enter the same Password as above." type="password" placeholder="Re-Enter Password" runat="server"/>
         </p>

         <p class="login button"> 
             <%--<asp:Button ID="btnSignup" runat="server" Text="Sign Up" OnClick="Button1_Click" Width="150px" CssClass="btnSignup" Height="46px" />&nbsp&nbsp&nbsp&nbsp&nbsp</p>--%>
             <asp:Button ID="btnSignup" runat="server" Text="Sign Up" onClientClick="return myFunction();" OnClick="btnSignup_Click" Width="150px" Height="40px"/>
         </p>

提交表单时不检查验证。还有其他方法可以调用JavaScript函数吗?

1 个答案:

答案 0 :(得分:1)

您是否收到任何javascript错误?打开浏览器控制台,您将看到Uncaught SyntaxErrors。