检查输入字段是否包含@字符? [英] Check if input field contains @ character?

查看:124
本文介绍了检查输入字段是否包含@字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查我的输入字段中是否包含@符号

Im trying to check if my input field contains an @ sign in it

所以这就是我所拥有的并且它不起作用:

so here is what i have and its not working:

$( "#Button" ).click(function() {
    if ($('#email:contains("@")')) {

    } else { 
        $('#email').text('Wrong');
    }
});

http://jsfiddle.net/xJtAV/70/

有什么想法吗?

推荐答案

:contains()返回符合条件的jQuery对象.它与值不匹配.

:contains() returns a jQuery object matching the criteria. It does not match value.

.val()上使用indexOf()

if ($('#email').val().indexOf('@') > -1) {

此外,在您的else{}中,请使用.val(),因为text()不会设置任何值.

Also, in your else{}, use .val() as text() does not set any value.

$('#email').val('Wrong');

但是我建议您使用另一个元素,例如<span id="emailError">

But I'd suggest you use another element say <span id="emailError">

更新了小提琴

Updated Fiddle

这篇关于检查输入字段是否包含@字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆