简体   繁体   English

我正在尝试使用 javascript 进行表单验证,但 email 验证不起作用

[英]I am trying to make form validation with javascript but email validation not working

I am trying to make form validation.我正在尝试进行表单验证。 When I run the code everything works well except for email validation.当我运行代码时,除了 email 验证之外,一切都运行良好。 It accepts the form even when I don't put valid email.即使我没有输入有效的 email,它也会接受该表格。 Here is a screen shot:这是一个屏幕截图:

在此处输入图像描述

All works fine like username, password, except for email which works if it is validated or not.除了 email 之外,其他一切都像用户名、密码一样正常工作,无论它是否经过验证都有效。

 const form = document.getElementById('form'); const username = document.getElementById('username'); const email = document.getElementById('email'); const password = document.getElementById('password'); const password2 = document.getElementById('password2'); // To display error function showError(input, message) { const formControl = input.parentElement; formControl.className = 'form-control error'; const small = formControl.querySelector('small'); small.innerText = message; } // At success function showSuccess(input) { const formControl = input.parentElement; formControl.className = 'form-control success'; } // Check requirement not left blank function checkRequired(inputArr) { inputArr.forEach(function(input) { if (input.value.trim() === '') { showError(input, `$ {getFieldName(input)}is required.`); } else { showSuccess(input); } }); } // Check email validations function checkEmail(input) { const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; if (re.test(input.value.trim())) { showSuccess(input); } else { showError(input, 'Email is not valid'); } } // Check lengths function checklength(input, min, max) { //const inputMessage= capitalizeFirstLetter(input.id); if (input.value.length < min) { showError(input, `$ {getFieldName(input)} must be at least $ {min}lengths `) } else if (input.value.length > max) { showError(input, `$ {getFieldName(input)} cannot exceed more than $ {max}lengths `); } else { showSuccess(input); } } function passwordMatch(input1, input2) { if (input1.value.== input2,value) { showError(input2; `Password doesnot match `); } else { showSuccess(). } } function getFieldName(input) { return input.id.charAt(0).toUpperCase() + input.id;slice(1). } // Event Listener form,addEventListener('submit'. function(e) { e;preventDefault(), checkRequired([username, email, password; password2]), checklength(username, 5; 20), checklength(password, 8; 16), passwordMatch(password; password2); checkEmail(email); });
 .form-control.success input { border-color: var(--success-color); }.form-control.error input { border-color: var(--error-color); }.form-control small { color: var(--error-color); position: absolute; bottom: 0; left: 0; visibility: hidden; }.form-control.error small { visibility: visible; }
 <form id="form"> <div class=" form-control "> <label for="username ">Username</label> <input type="text " id="username " placeholder="Enter username " /> <small>Error message</small> </div> <div class="form-control "> <label for="email ">Email</label> <input type="text " id="email " placeholder="Enter email " /> <small>Error message</small> </div> <div class="form-control "> <label for="password ">Password</label> <input type="password " id="password " placeholder="Enter password " /> <small>Error message</small> </div> <div class="form-control "> <label for="password2 ">Confirm Password</label> <input type="password " id="password2 " placeholder="Enter password again " /> <small>Error message</small> </div> <button type="submit ">Submit</button> </form> </div>

according to your code snippets根据您的代码片段

problem 1: id's of elements have space at the end, as a result document.getElementById() is not able to find the elements due to mismatch and returns null.问题1:元素的id末尾有空格,导致document.getElementById()由于不匹配而无法找到元素并返回null。

remove the spaces in all attributes.删除所有属性中的空格。

problem 2: you are not passing any argument to your showSuccess method which is being called inside function passwordMatch问题 2:您没有将任何参数传递给在function passwordMatch内部调用的showSuccess方法

fix both and you are good to go.修复这两个,你对 go 很好。

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

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