简体   繁体   English

JavaScript 表单验证(必需)

[英]JavaScript Form Validation (required)

Hello I am building a website for myself and came across this issue of my form going directly to its success page with empty inputs.您好,我正在为自己建立一个网站,遇到了这个问题,我的表单直接进入了它的成功页面,输入为空。 How can I fix this?我怎样才能解决这个问题? I already scanned through other questions like mine and none have seemed to solve this issue.我已经浏览了像我这样的其他问题,但似乎没有一个可以解决这个问题。 The form is within HTML tag and the inputs closed '/>'.该表格位于 HTML 标记内,输入关闭“/>”。 I think this could be my script affecting it but I don't understand the concept as to how exactly should I go about coding to 'validate' a form.我认为这可能是我的脚本影响它,但我不明白我应该如何 go 关于编码以“验证”表单的概念。 I understand you can do so with JavaScript and JQuery.我知道您可以使用 JavaScript 和 JQuery 来做到这一点。 With that said how do I fix this issue of mine here are the codes snippet.话虽如此,我该如何解决我的这个问题,这里是代码片段。

<div class="contact-form col-md-6 col-sm-6">
                    <form action="#" name="contact" class="needs-validation">
                        <label for="fname">Full Name</label>
                        <input type="text" id="fname" name="firstname" placeholder="Enter your full name..." required />
                        <label for="email">Email</label>
                        <input type="text" id="email" name="email" placeholder="Enter a valid email..." required />
                        <label for="subject">Message</label>
                        <textarea id="subject" name="subject" placeholder="Your requested choice/inquiry..." required></textarea>
                        <a href="success.html" class="button-submit">SEND</a>
                    </form>
</div>

And here is the script I was trying to work on:这是我试图处理的脚本:

// Disable form submissions if there are invalid fields
(function() {
    'use strict';
    window.addEventListener('load', function() {
        // Get the forms we want to add validation styles to
        var forms = document.getElementsByClassName('needs-validation');
        // Loop over them and prevent submission
        var validation = Array.prototype.filter.call(forms, function(form) {
            form.addEventListener('submit', function(event) {
                if (form.checkValidity() === false) {
                    event.preventDefault();
                    event.stopPropagation();
                }
                form.classList.add('was-validated');
            }, false);
        });
    }, false);
})();

Your help would be appreciated, thank you.您的帮助将不胜感激,谢谢。

You code is not right, it shouldn't be like this, a with href will always redirect you to the specified page instead of doing post or get.您的代码不正确,不应该是这样的,带有href将始终将您重定向到指定的页面,而不是进行发布或获取。

Change this code:更改此代码:

 <a href="success.html" class="button-submit">SEND</a>

To this:对此:

 <button class="btn btn-primary" type="submit">SEND</button>

Your HTML should look like this:您的 HTML 应如下所示:

<div class="contact-form col-md-6 col-sm-6">
    <form method="post" action="#" name="contact" class="needs-validation">
        <label for="fname">Full Name</label>
        <input class="form-control" type="text" id="fname" name="firstname" placeholder="Enter your full name..." required />
        <label for="email">Email</label>
        <input class="form-control" type="text" id="email" name="email" placeholder="Enter a valid email..." required />
        <label for="subject">Message</label>
        <textarea class="form-control" rows="3" id="subject" name="subject" placeholder="Your requested choice/inquiry..." required></textarea><br>
        <button type="submit"class="btn btn-success">SEND</button>
    </form>
</div>

THERE IS NOTHING WRONG WITH YOUR SCRIPT.您的脚本没有任何问题。 You have to learn the html structure properly.您必须正确学习 html 结构。

You need to prevent your default right at the start of your onsubmit function.您需要在 onsubmit function 开始时阻止您的默认设置。 While your checkValidity function is running, the form has probably finished processing and then submitting.当您的 checkValidity function 正在运行时,表单可能已完成处理然后提交。 Preventing it this high will just stop it from processing all together so you can then check your validation and do what you want depending on the result.阻止它这么高只会阻止它一起处理,因此您可以检查您的验证并根据结果执行您想要的操作。

form.addEventListener('submit', function(event) {
   event.preventDefault();
   if(form.checkValidity()){
      form.classList.add('was-validated');
   }
}, false);

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

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