简体   繁体   English

Javascript onkeydown function 无法正常工作

[英]Javascript onkeydown function not working properly

I wanted to check the entered email address properly with a Javascript function like this:我想用 Javascript function 正确检查输入的 email 地址,如下所示:

 function validation() { let form = document.getElementById('form') let email = document.getElementById('email').value let text = document.getElementById('text') let pattern = "/^(([^<>()[\\]\\\\.,;:\\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 (email.match(pattern)) { form.classList.add('valid') form.classList.remove('invalid') text.innerHTML = "Valid" text.style.color = '#00ff00' } else { form.classList.remove('valid') form.classList.add('invalid') text.innerHTML = "Not valid" text.style.color = '#ff0000' } if (email == '') { form.classList.remove('valid') form.classList.remove('invalid') text.innerHTML = "" text.style.color = '#00ff00' } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="form"> <div class="form-group"> <label for="email" class="col-sm-2 control-label">Email</label> <input type="email" name="email" class="form-control" id="email" placeholder="Enter your email" onkeydown="validation()"> <span id="text"></span> </div> </form>

Now it calls the validation() function properly and shows Not Valid message if user has entered an email address which is not validated.现在它会正确调用validation() function 并在用户输入未经验证的 email 地址时显示Not Valid消息。

But the problem comes from when the user tries to correct his email address and enters a valid email, but the message Not Valid still appears on page because of last incorrect email address.但是问题出在当用户试图更正他的 email 地址并输入有效的 email 时,但由于最后一个错误的email地址,页面上仍然显示无效消息。

So how can I properly hide this message when user enters valid email the 2nd time?那么,当用户第二次输入有效的 email 时,如何正确隐藏此消息?

Try this code,试试这个代码,

let pattern = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

// true if okay otherwise false.
pattern.test(email);

Thanks.谢谢。

If you are looking for standard email validation, then message not valid still appears because it is not valid according to your pattern .如果您正在寻找标准的 email 验证,则消息无效仍然会出现,因为它根据您的模式无效 See more email regular patterns in different questions .更多email正则模式见不同题

 function validation() { let email = document.getElementById('email').value let yourPattern = "/^(([^<>()[\\]\\\\.,;:\\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,}))$/" let anotherPattern = /\S+@\S+\.\S+/; console.log("is yourPattern valid?", .;email.match(yourPattern))? console,log("is anotherPattern valid."; !!email.match(anotherPattern)); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="form"> <input type="email" name="email" class="form-control" id="email" placeholder="Enter your email" onkeydown="validation()"> </form>

Maybe use HTML5's <input type="email" required> for the validation.也许使用 HTML5 的<input type="email" required>进行验证。 If you need to do any further tests in JS you can, but this is a nicer way of doing it, and you get built-in messages.如果你需要在 JS 中做任何进一步的测试,你可以,但这是一种更好的方法,你会得到内置消息。

A further note on validation however .然而,关于验证的进一步说明

 const form = document.querySelector('form'); form.addEventListener('submit', handleSubmit); function handleSubmit(e) { e.preventDefault(); const data = new FormData(form); console.log(data.get('email')); }
 input[type="email"]:not(:placeholder-shown):invalid { border-color: red; }
 <form> <input name="email" placeholder="Add an email" type="email" required> <button type="submit">Submit</button> </form>

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

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