简体   繁体   English

使用 Javascript 验证后,HTML 表单不会提交

[英]HTML Form won't submit after validaiton with Javascript

I have questions regarding form submitting in HTML with Javascript.我对使用 Javascript 以 HTML 格式提交表单有疑问。

My Javascript is as follows:我的Javascript如下:

function validateForm() {
        var a= document.forms["myForm"]["pname"].value;
        var b = document.forms["myForm"]["pemail"].value;
        var c = document.forms["myForm"]["pdob"].value;
        var d = document.forms["myForm"]["unit of choice"].value;
        var els = document.forms["myForm"].elements["Issue[]"];
        var f = document.forms["myForm"]["description"].value;
        var g = document.forms["myForm"]["pdatee"].value;
        var h = document.forms["myForm"]["ptimee"].value;

        var isValid = false;
        for (i = 0; i < els.length; i += 1) {
        if (els[i].checked) {
            isValid = true;
            }
        }


        if (a == null || a == "") {
            alert("Your name cannot be blank");
            }
        if (b == null || b == "") {
            alert("Enter a valid email address.");
            }
        if (c == null || c == "") {
            alert("Enter a valid Date of Birth. (dd/mm/yyyy)");
            }
        if (d == null || d == "") {
            alert("Unit and Tutor have to be selected.");
            }

        if (!isValid) {
            alert("Must select an Issue.");
            }

        if (f == null || f == "") {
            alert("Must fill in a description.");
            }

        if (f == null || f == "") {
            alert("Must fill in a description.");
            }
        if (g == null || g == "") {
            alert("Preferred date must follow the format set.");
            }
        if (h == null || h == "") {
            alert("Preferred time must follow the format set.");
            }

        return false;


    }

And this is my form with its attributes in HTML:这是我的表单,其属性为 HTML:

<form name="myForm" onsubmit="return validateForm()" method="post" action="confirm.html" novalidate="novalidate" >

What happens is that when I click the Submit button after filling in all the requirements(so everything does not return false) , the form won't submit itself.发生的情况是,当我在填写所有要求后单击“提交”按钮(因此所有内容都不会返回 false)时,表单不会自行提交。

After reading around regarding return false, I tried adding else{ return true;在阅读了有关 return false 的内容后,我尝试添加 else{ return true; } but all it does is submit my form without validation at all.但它所做的只是在没有验证的情况下提交我的表单。

What do I do to make it work with only Javascript and/or HTML?我该怎么做才能使其仅使用 Javascript 和/或 HTML? Thank you!谢谢!

You need to check if your inputs have passed your tests.您需要检查您的输入是否通过了测试。 You can do that by using your var isValid .您可以通过使用 var isValid来做到这一点。

All you need to do is set isValid to false if one of your conditions was not met, and then return isValid .如果您的条件之一不满足,您需要做的就是将isValid设置为false ,然后返回isValid

function validateForm() {
    var a= document.forms["myForm"]["pname"].value;
    var b = document.forms["myForm"]["pemail"].value;
    var c = document.forms["myForm"]["pdob"].value;
    var d = document.forms["myForm"]["unit of choice"].value;
    var els = document.forms["myForm"].elements["Issue[]"];
    var f = document.forms["myForm"]["description"].value;
    var g = document.forms["myForm"]["pdatee"].value;
    var h = document.forms["myForm"]["ptimee"].value;

    var isValid = false;
    for (i = 0; i < els.length; i += 1) {
    if (els[i].checked) {
        isValid = true;
        }
    }


    if (a == null || a == "") {
        isValid=false;
        alert("Your name cannot be blank");
        }
    if (b == null || b == "") {
        isValid=false;
        alert("Enter a valid email address.");
        }
    if (c == null || c == "") {
        isValid=false;
        alert("Enter a valid Date of Birth. (dd/mm/yyyy)");
        }
    if (d == null || d == "") {
        isValid=false;
        alert("Unit and Tutor have to be selected.");
        }

    if (!isValid) {
        isValid=false;
        alert("Must select an Issue.");
        }

    if (f == null || f == "") {
        isValid=false;
        alert("Must fill in a description.");
        }

    if (f == null || f == "") {
        isValid=false;
        alert("Must fill in a description.");
        }
    if (g == null || g == "") {
        isValid=false;
        alert("Preferred date must follow the format set.");
        }
    if (h == null || h == "") {
        isValid=false;
        alert("Preferred time must follow the format set.");
        }

    return isValid;


}

At the end of the submission function, you are returning:在提交功能结束时,您将返回:

    return false;
}

No matter if the validation was successful or not.不管验证成功与否。 This prevents the form from submitting.这会阻止表单提交。 Make sure you are using return true here and correctly validate using a flag and then if the flag is true , return false .确保您在此处使用return true并使用标志正确验证,然后如果标志为true ,则return false

You have already a flag isValid .您已经有一个标志isValid Replace the above line with:将上面的行替换为:

    return isValid;
}

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

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