javascript 使用 jQuery 在 keyup 上验证电子邮件地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19657032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:22:34  来源:igfitidea点击:

Validate email address on keyup with jQuery

javascriptjquery

提问by Liam

Im trying to validate an email address onkeyup only the following doesn't ever seem to return valid,

我试图验证一个电子邮件地址 onkeyup 只有以下似乎永远不会返回有效,

Can anybody see where i may be going wrong? I only have 1 input and my .first()selector is targeting that correctly...

任何人都可以看到我可能出错的地方吗?我只有 1 个输入,而我的.first()选择器的目标是正确的...

$('.newsletter-signup input').first().keyup(function(){
    $email = $('.newsletter-signup input').first();
     function validateEmail($email) {
      var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
      if( !emailReg.test( $email ) ) {
        alert('foo');
      } else {
        alert('bar');
      }
    }
});

回答by pala?н

You code is bit incorrect, you can do this:

你的代码有点不正确,你可以这样做:

$('.newsletter-signup input').first().keyup(function () {
    var $email = this.value;
    validateEmail($email);
});

function validateEmail(email) {
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    if (!emailReg.test(email)) {
        alert('foo');
    } else {
        alert('bar');
    }
}

In your code:

在您的代码中:

$email = $('.newsletter-signup input').first();

you are only getting the jQuery object of the first input not the value. Hence the validation is not working at all.

你只得到第一个输入的 jQuery 对象而不是值。因此,验证根本不起作用。

So, in order to get the value of the inputelement you can use:

因此,为了获取input元素的值,您可以使用:

 var $email = this.value;

as thishere refers to the $('.newsletter-signup input').first(). So need to again call it and after that get the value from it. You can directly do that using this.value.

因为this这里指的是$('.newsletter-signup input').first()。所以需要再次调用它,然后从中获取值。您可以直接使用this.value.

回答by adeneo

That's a lot of code to do very little, when all you're really trying to do is this:

这是很多代码要做的很少,而您真正想做的是:

$('.newsletter-signup input:first').on('keyup', function(){
    var valid = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/.test(this.value);
    alert(valid ? 'foo' : 'bar');
});

Also note that setting the input type to "email" does the validation for you in browsers that support HTML5.

另请注意,将输入类型设置为“电子邮件”会在支持 HTML5 的浏览器中为您进行验证。

FIDDLE

小提琴

回答by Rituraj ratan

$('.newsletter-signup input').first().keyup(function(){
    $email = $(this).val();// use val here to get value of input
     validateEmail($email);
});

   function validateEmail($email) {
          var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
          if( !emailReg.test( $email ) ) {
            alert('foo');
          } else {
            alert('bar');
          }
        }

reference .val()

参考.val()