Mousedown仍在提交表单,但不应该提交 [英] Mousedown still submitting form, when it should not

查看:67
本文介绍了Mousedown仍在提交表单,但不应该提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个登录验证表单,该表单使用jquery和ajax的组合进行验证...如果值确定,则应提交表单;如果值确定,则应不提交表单...但是就我而言,即使值不正确(我正在使用mousedown函数),表单仍在提交,请参见下面的代码.

hello i have a login validation form which uses a mix of jquery and ajax to do validations... if the values are ok the form should submit, if the values are not ok then the form should not submit... however in my case the form is submitting even when the values are incorrect ( i am using the mousedown function ) please see below my code..

<form method="post" name="loginform" action="models/login.php">
    <input type="email" class="homepage" name="user_email2" id="user_email2" placeholder="Email" maxlength="50" />
    <div class="errormsg" id="errormsg6"></div>
    <input type="password" class="homepage" name="user_password2" id="user_password2" placeholder="Password" maxlength="20" />
    <div class="errormsg" id="errormsg7"></div>
    <input type="submit" name="login" id="login" value="Submit">
    <div class="errormsglast" id="errormsg8"></div>
</form>

jquery和ajax

jquery and ajax

$(document).ready(function()
{
    /* ----------------- Login Validations Global Variables -----------------   */
    var user_email2 = "";
    var user_emailajax2 = "";
    var user_password2 = "";
    var user_passwordajax2 = "";
    var emailformat = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);

    /* ----------------- Define Validate Email */
    var validate_email_login = function()
    {
        var item5 = $("#user_email2").val().toLowerCase();
        if (item5.length < 6 || item5.length > 50)
        {
            $("#errormsg6").html("Email : 6 - 50 Characters");
            user_email2 = "";
        }
        else
        {
            $("#errormsg6").html("");
            user_email2 = item5;
            if (!emailformat.test(item5))
            {
                $("#errormsg6").html("Wrong Email Format");
                user_email2 = "";
            }
            else
            {
                $("#errormsg6").html("");
                user_email2 = item5;
                $.ajax(
                {
                    type: 'POST',
                    url: 'classes/validatelogin.php?f=1',
                    data: "user_email2=" + item5,
                    success: function(msg)
                    {
                        if (msg == "ok")
                        {
                            user_emailajax2 = "";
                            $("#errormsg6").html("Email Does Not Exist");
                        }
                        else if (msg == "exists")
                        {
                            user_emailajax2 = item5;
                            $("#errormsg6").html("");
                        }
                    }
                });
            }
        }
    }

    /* ----------------- Define Validate Password */
    var validate_password_login = function()
    {
        var item5 = $("#user_email2").val().toLowerCase();
        var item6 = $("#user_password2").val();

        if (item6.length < 8 || item6.length > 20)
        {
            $("#errormsg7").html("Password : 8-20 Characters");
            user_password2 = "";
        }
        else
        {
            $("#errormsg7").html("");
            user_password2 = item6;
            if (user_email2 != "" && user_emailajax2 != "")
            {
                $.ajax(
                {
                    method: "POST",
                    url: "classes/validatelogin.php?f=2",
                    data: "user_email2=" + item5 + "&user_password2=" + item6,
                    success: function(msg)
                    {
                        if (msg == "WrongPw")
                        {
                            user_passwordajax2 = "";
                            $("#errormsg7").html("Wrong Password - See Forgot Password");
                        }
                        else if (msg == "CorrectPw")
                        {
                            user_passwordajax2 = item6;
                            $("#errormsg7").html("");
                            /* window.location.href="manage-properties"; */
                        }
                    }
                });
            }
        }
    }




    /* ----------------- Run Functions */
    $("#user_email2").on('focusout', validate_email_login);
    $("#user_password2").on('focusout', validate_password_login);


    /* ----------------- Stop on Submit */
    $( "#login" ).mousedown(function() 
    {
        validate_email_login();
        validate_password_login();


        if (user_email2 == "" || user_emailajax2 == "" || user_password2 == "" || user_passwordajax2 == "")
        {
            $("#errormsg8").html("Please Fill All Fields (Correctly)");
            console.log("submit false");
            return false;
        }
        else
        {
            $("#errormsg8").html("");
            console.log("submit true");
            return true;
        }
    });
});

尝试过的解决方案-问题是,当用户输入错误的事件就可以了,但是如果用户随后输入了正确的值,则提交将在第一次时返回false,然后第二次返回true. ..它应该首先返回true

Solution Tried - problem is that when user puts the wrong event that is fine, but if user then puts the correct values, the submit returns false on first time, then second time it returns true... it should return true in first go

<input type="button" name="login" id="login" value="Submit">

    $( "#login" ).mousedown(function() 
    {
        validate_email_login();
        validate_password_login();


        if (user_email2 == "" || user_emailajax2 == "" || user_password2 == "" || user_passwordajax2 == "")
        {
            $("#errormsg8").html("Please Fill All Fields (Correctly)");
            console.log("submit false");
            return false;
        }
        else
        {
            $("#errormsg8").html("");
            console.log("submit true");
            $('[name=loginform]').submit();
        }
    });
});

推荐答案

仅具有普通按钮(例如<input type="button" name="login" id="login" value="Submit">),而不是具有type ="submit"按钮.然后,当您完成检查值并满意地发送它时,只需调用:

Instead of having a type="submit" button just have a normal button e.g<input type="button" name="login" id="login" value="Submit">. Then when you finished checking the values and happy that it should send then just call:

$('[name=loginform]').submit();

因为当前正在发生的事情是您单击该按钮时提交了表单,因为您没有阻止该事件的发生.

Because what is happening currently is that the form submits when you click on the button, because you are not stopping that event from happening.

如果您想阻止表单提交,我建议您不要使用该按钮,而是像上面提到的那样自行启动提交,或者您也可以在表单元素上使用onsubmit ="someFunction()"如果不应提交,则返回false;如果应提交,则返回true.

If you want to prevent the form from submitting I would suggest either not using that button and initiating the submit yourself like I mentioned above, or alternatively you can use the onsubmit="someFunction()" on the form element way and just return false if it should not submit and return true if it should.

这篇关于Mousedown仍在提交表单,但不应该提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆