简体   繁体   English

使用JavaScript进行电子邮件输入即时验证

[英]Email input instant validation with javascript

I'm trying to validate input type="email" with js . 我正在尝试使用js验证输入type="email"

I found different ways to do that but non of them works form me. 我找到了不同的方法来完成这项工作,但我没有一个能奏效。

HTML HTML

<label for="email" class="form__label">User e-mail:</label>
<input type="email" name="email" id="email" onblur="checkEmail(this.value);" class="form__input" >

JS JS

  document.addEventListener('DOMContentLoaded', function() {

  var email = document.getElementById('email');

  function checkEmail(email) {
    var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;

    if (reg1.test(email) == false) {
      email.style.border = "1px solid red";
    }else (reg1.test(email) == true) {
      email.style.border = "1px solid green";
    }
  };

});

What I get every time is: 我每次都能得到的是:

Uncaught ReferenceError: checkEmail is not defined at HTMLInputElement.onblur . .. ..

I know, I make some simple error but I'm struggling with this for a while now and can't move on... 我知道,我犯了一些简单的错误,但是我为此苦了一段时间,无法继续前进...

You are missing If keyword in your code ie 您在代码中缺少If关键字,即

else (reg1.test(email) == true)

 var email = document.getElementById('email'); function checkEmail(element) { var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[az]{2,4}$/; if (reg1.test(email.value) == false) { email.style.border ='1px solid red'; }else if(reg1.test(email.value) == true){ email.style.border = "1px solid green"; } }; 
 <label for="email" class="form__label">User e-mail:</label> <input type="email" name="email" id="email" onblur="checkEmail(this);" class="form__input" > 

You could ineed use the Email Check of HTML5 as quoted by @Andriy Klitsuk. 您可以使用@Andriy Klitsuk引用的HTML5电子邮件检查功能。 So you could completely omit your extra checkEmail call. 因此,您可以完全省去多余的checkEmail呼叫。

But getting to your error : 但是要解决您的错误:

Uncaught ReferenceError: checkEmail is not defined at HTMLInputElement.onblur 未捕获的ReferenceError:在HTMLInputElement.onblur上未定义checkEmail

you get the above error as the checkEmail function is present inside EventHandler document.addEventListener('DOMContentLoaded', function(){ .... and hence is not available outside it. 你收到上述误差作为checkEmail功能是本事件处理程序内部document.addEventListener('DOMContentLoaded', function(){ .... ,并因此不可用外面。

You could take the function out of the EventListener as it is not being called inside the EventHandler anyway : 您可以将函数从EventListener中取出,因为无论如何在EventHandler中都不会调用它:

  document.addEventListener('DOMContentLoaded', function() {

  var email = document.getElementById('email');
  });
  function checkEmail(email) {
    var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;

    if (reg1.test(email) == false) {
      email.style.border = "1px solid red";
    } else if (reg1.test(email) == true) { // if was missing here
      email.style.border = "1px solid green";
    }
  };

I would prefer passing the element 我更喜欢传递元素

<label for="email" class="form__label">User e-mail:</label>
<input type="email" name="email" id="email" onblur="checkEmail(this);" class="form__input" >

And the function would get the value as follows 并且该函数将获得如下值

function checkEmail(email) {
    var val = email.value;
    var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;

    if (reg1.test(email) == false) {
      email.style.border = "1px solid red";
    }else if (reg1.test(email) == true) {
      email.style.border = "1px solid green";
    }
  };

also an if is missing 还有一个if是否丢失

code pen for reference https://codepen.io/YasirKamdar/pen/eVRaYZ?editors=1111 参考代码笔https://codepen.io/YasirKamdar/pen/eVRaYZ?editors=1111

You are missing the condition as well. 您也缺少条件。 simply use this. 简单地使用这个。

  function checkEmail(elm) {
    var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
console.log(reg1.test(elm.value))
    if (reg1.test(elm.value) == false){
      elm.style.border = "1px solid red";
    }else{
     elm.style.border = "1px solid green";
    }
  }

<label for="email" class="form__label">User e-mail:</label>
<input type="email" name="email" id="email" onblur="checkEmail(this);" class="form__input" >

First off, there is a syntax error in your else statement. 首先, else语句中存在语法错误。 It should be else if . 如果是,应该是else if Now to the solution; 现在解决; remove the checkEmail() function from the eventListener and let it stand alone, like this: 除去checkEmail()从功能eventListener ,让它独立的,就像这样:

function checkEmail(email) {
    var email = document.getElementById('email');
    var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;

    if (reg1.test(email) == false) {
      email.style.border = "1px solid red";
    }else if (reg1.test(email) == true) {
      email.style.border = "1px solid green";
    }
  }

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

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