简体   繁体   English

电子邮件验证表单错误

[英]Email validation form errors

I am trying to create a relatively simple contact form and failing :( 我正在尝试创建一个相对简单的联系表格,但失败了:(

I am following this https://pageclip.co/blog/2018-02-20-you-should-use-html5-form-validation.html and the first step using JS is to set the outline the box with a red border when an invalid imput is entered. 我正在关注这个https://pageclip.co/blog/2018-02-20-you-should-use-html5-form-validation.html ,使用JS的第一步是将边框设置为带有红色边框的轮廓输入无效的输入时。

However using the below code isn't doing anything. 但是,使用下面的代码不会执行任何操作。

 const inputs = document.querySelectorAll("input, select, textarea"); inputs.forEach(input => { input.addEventListener( "invalid", event => { input.classList.add("error"); }, false ); }); 
 /*Email Validation*/ input.invalid { border-color: red; } 
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous"> <h1 class="text-center font-bold mt-5 pt-2 mb-3"> <em>Contact us</em> </h1> <!-- contact--> <section id="contact" style="background-color: #fff;" class="text-page"> <div class="container"> <div class="row"> <div class="col-md-12"> <h2 class="heading">Contact</h2> <div class="row"> <div class="col-md-6"> <form id="contact-form" method="post" action="#" class="contact-form"> <div class="controls"> <div class="row"> <div class="col-sm-6"> <div class="form-group"> <label for="name">Your firstname *</label> <input type="text" name="name" placeholder="Enter your firstname" required="required" class="form-control"> </div> </div> <div class="col-sm-6"> <div class="form-group"> <label for="surname">Your lastname *</label> <input type="text" name="surname" placeholder="Enter your lastname" required="required" class="form-control"> </div> </div> </div> <div class="form-group"> <label for="surname">Your email *</label> <input type="email" name="email" placeholder="Enter your email" required="required" class="form-control"> </div> <div class="form-group"> <label for="surname">Your message for us *</label> <textarea rows="4" name="message" placeholder="Enter your message" required="required" class="form-control"></textarea> </div> <div class="text-center"> <input type="submit" name="name" value="Send message" class="btn btn-primary btn-block"> </div> </div> </form> </div> <div class="col-md-6"> <p class="social"><a href="#" title="" class="facebook"><i class="fa fa-facebook"></i></a><a href="#" title="" class="twitter"><i class="fa fa-twitter"></i></a><a href="#" title="" class="gplus"><i class="fa fa-google-plus"></i></a><a href="#" title="" class="instagram"><i class="fa fa-instagram"></i></a><a href="#" title="" class="email"><i class="fa fa-envelope"></i></a></p> </div> </div> </div> </div> </div> </section> 

The code above has a syntax error, you need to change the name of input.invalid to input.error 上面的代码有语法错误,您需要将input.invalid的名称更改为input.error

/*Email Validation*/


input.invalid {
 border-color: red;
}

to

input.error{
  border-color: red;
}

Try vanilla JS's .checkCustomValidity . 试试香草JS的.checkCustomValidity Something like the below fired on a .keyup event attached to the entire form works well. 在附加到整个表单的.keyup事件上.keyup的如下所示的效果很好。 NOTE: I just hand coded this so you get the idea... It may need some tweaking. 注意:我只是手工编码了一下,这样您就可以理解...可能需要进行一些调整。

Then you set your "Submit" button to disabled or enabled based on validity. 然后,根据有效性将“提交”按钮设置为禁用或启用。

function formIsValid(formId){
var result = true;
var formInputs = document.getElementById(formId).querySelectorAll("input");
    for (var i = 0; i < formInputs.length; i++) {
        //if the input is not hidden or disabled
        if(formInputs[i].offsetHeight > 0 && !formInputs[i].attribute("disabled","disabled")){
            if(!dialogInputs[i].checkValidity()){
                result = false;
            }
        }
    }
    return result;
}

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

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