简体   繁体   English

阻止用户提交

[英]prevent the user from submitting

I want to prevent the user from submitting data in a form, but when I test it with JavaScript it's not returning true. 我想防止用户以表单形式提交数据,但是当我使用JavaScript测试数据时,它没有返回true。

this is the submit button : 这是提交按钮:

    input type="submit" value="S'inscrire" name="inscrire" onsubmit="return Verifier(this.form);">

and this is the code for the JS test function : 这是JS测试功能的代码:

 function Verifier()
        {
            var mdp1 = document.form.mdp.value,
                        mdp2 = document.form.confirmer.value,
                        email = document.form.email.value,
                        pseudo = document.form.pseudo.value;

            var testMdpIdentique = (function() {

                    if(mdp1 == mdp2) return true;
                    else return false;
                 })();


            if(!testMdpIdentique || mdp1 == "" || mdp2 == "" || email == "" || pseudo== "" )
            {
                alert ('Il faut remplir tous les champs');
                return false;
            }
            return true;

        }

the problem is that it's submitting information even if the test is not valid, I tried to try an alert message in the Valider function but it didn't worked. 问题是,即使测试无效,它也正在提交信息,我尝试在Valider函数中尝试一条警报消息,但没有成功。

In normal Javascript 用普通的Javascript

you can use return value of function to prevent form submission 您可以使用函数的返回值来防止表单提交

<form name="myForm" onsubmit="return validateMyForm();"> 

and function like 和功能像

<script type="text/javascript">
function validateMyForm()
{
  if(check if your conditions are not satisfying)
  { 
    alert("validation failed false");
    returnToPreviousPage();
    return false;
  }

  alert("validations passed");
  return true;
}
</script>

In jQuery 在jQuery中

$('#form').submit(function (evt) {
    evt.preventDefault();
    window.history.back();
});

In DOJO 在DOJO中

dojo.connect(form, "onsubmit", function(evt) {
    evt.preventDefault();
    window.history.back();
});

I think the below code is syntactically incorrect. 我认为以下代码在语法上不正确。

var testMdpIdentique = (function() {

                if(mdp1 == mdp2) return true;
                else return false;
             })();

Replace with 用。。。来代替

var testMdpIdentique = function() {

                if(mdp1 == mdp2) return true;
                else return false;
             };

Also you don't need to pass (this.form) in the onClick event. 另外,您不需要在onClick事件中传递(this.form)。 Generally submit button submits the form is there is a syntax error in your onClick callback method. 通常,提交按钮提交表单的onClick回调方法中有语法错误。

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

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