简体   繁体   English

jQuery按键验证电子邮件输入

[英]Jquery keypress to validate email inputs

What I'd like to accomplish is this: 我想要完成的是:

If a user types in a valid email, display the ( .check )'ok' sign. 如果用户输入了有效的电子邮件,请显示( .check )“确定”符号。 If not valid, display nothing(for the time being. I'll put something in later). 如果无效,则不显示任何内容(暂时。稍后再输入)。 I have 3 email fields. 我有3个电子邮件字段。 I'm trying to 'validate' for each one. 我正在尝试为每个人“验证”。

<form id="emailsForm" method="POST" action="/account/recommend/">
    <div class="prepend">
        <p><strong>Emails:</strong></p>
        <div>
            <span class="added"><p class="check">ok</p></span>
            <input type="email" id="email1" class="input-text" onblur="alert(/([A-Z0-9a-z_-][^@])+?@[^$#<>?]+?\.[\w]{2,4}/.test(this.value))">
        </div>
        <div>
            <span class="added"><p class="check">ok</p></span>
            <input type="email" id="email2" class="input-text">
        </div>
        <div>
            <span class="added"><p class="check">ok</p></span>
            <input type="email" id="email3" class="input-text">
        </div>
        <button type="submit" class="submit">Send</button>
    </div>
</form>

$("form#emailsForm :input").each(function(){
    $('input').blur(function() {
        var testEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
        if (testEmail.test(this.value)){
            $('input').siblings(".check").css('visibility', 'visible');
        }
        else {

        }
    });
});

http://jsfiddle.net/RFcaN/23/ What am I doing wrong? http://jsfiddle.net/RFcaN/23/我在做什么错?

Thanks for your help and suggestions! 感谢您的帮助和建议!

First off you need to find the sibling of this input, so change your selector from $('input') to $(this) . 首先,您需要找到this输入的同级,因此将选择器从$('input')更改$(this)

Secondly, .check is not a sibling of the input. 其次, .check不是输入的同级。 It's a descendant of the sibling. 这是兄弟姐妹的后代。

$(this).siblings(".added").find('.check').css('visibility', 'visible');

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

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