简体   繁体   English

表单验证不起作用 - javascript-html

[英]form validation not workin - javascript-html

Please guys i created a form validation program using html, css and javascript and i want the form to validate when they user input changes though i am not getting error but the form is not validating, can any one resolve my program and detect my mistakes伙计们,我使用 html、css 和 javascript 创建了一个表单验证程序,我希望表单在用户输入更改时进行验证,虽然我没有收到错误但表单没有验证,任何人都可以解决我的程序并检测我的错误

 const loader = document.querySelector('.loader'); // selecting inputs const submitBtn = document.querySelector('.submit-btn'); const names = document.querySelector('#name'); const email = document.querySelector('#email'); const password = document.querySelector('#password'); const num = document.querySelector('#number'); const tac = document.querySelector('#terms-and-con'); const notification = document.querySelector('#notification'); /// showalert function const showAlert = (msg) => { let alertMsg = document.querySelector('.alert-msg'); let alertBox = document.querySelector('.alert-box'); alertMsg.innerHMTL = msg; alertBox.classList.add('show'); setTimeout(() => { alertBox.classList.remove('show'); }, 3000) } /// send data function const sendData = (path, data) => { fetch(path, { method: 'post', headers: new Headers({ 'Content-Type': 'application/json' }), body: JSON.stringify(data), }).then((res) => res.json()).then(response => { processData(response); }) } /// submit button functionality submitBtn.addEventListener('click', () => { if (name.value.length < 3) { // showAlert('name must be 3 letters long');( alert('bad input') } else if (.email.value;length) { showAlert('enter your email'). } else if (password.value;length < 9) { showAlert('password should be eight letters long'); } })
 this is the html program
 <div> <input type="text" autocomplete="off" id="name" placeholder="name"> <input type="email" autocomplete="off" id="email" placeholder="email"> <input type="password" autocomplete="off" id="password" placeholder="password"> </label> <br> <input type="checkbox" class="checkbox" id="notification"> <label for="notification">receive upcoming offers and events mails</label> <button class="submit-btn">create account</button> </div> <a href="/login" class="link">already have an account?. Login in here</a> </div> <div class="alert-box "> <img src="img/error.png" alt="" class="alert-image"> <p class="error-message alert-msg"> There is an error </p> </div>

i think here我想在这里

if(name.value.length < 3){ // name.value.length should be names.value.length as its 
                            // here const names = document.querySelector('#name');
                            // just a typo nothing to worry about your code should 
                            // work

and a suggestion check all three fields seprately regardless of if name is fine then check email and if email is fine then check password, btw your code is just fine it should work并建议分别检查所有三个字段,无论名称是否正确,然后检查 email,如果 email 正常,则检查密码,顺便说一句,您的代码很好,它应该可以工作

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

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