How to verify "Contains" using JavaScript

Possible Duplicate:
JavaScript: string contains

In the below code I have to check whether the textbox contains @ and . . I tried writing the below code which is not working. Anybody please do provide me with the correct syntax.

    function emailval(objval) {
        if (obj.value.contains("@") && obj.value.contains(".")) {
            document.getElementById(mid).style.backgroundColor = "#BDD470";
        }
        else {
            document.getElementById(mid).style.backgroundColor = "#CC0505";
        }
    }

</script>

Enter text (Enter your email):
<form id="form1" runat="server">
<br />
<br />
<asp:TextBox ID="TextBox1" onkeyup="return emailval(this)" runat="server"></asp:TextBox>
<br />
<br />
</form>

你需要 -

if (obj.value.indexOf("@") != -1 && obj.value.indexOf(".") != -1)

对于本地字符串包含使用indexOf:

 str.indexOf('@') !== -1; // str contains '@' true

或正则表达式的方式...只是因为它在那里

if(obj.value.match(/[.].*[@]|[@].*[.]/))
链接地址: http://www.djcxy.com/p/2058.html

上一篇: 如何检查文件名是否包含JavaScript中的子字符串?

下一篇: 如何使用JavaScript验证“包含”