开发者

How to verify "Contains" using JavaScript [duplicate]

This question already has answers here: Closed 11 years ago.

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>


For native string contains use indexOf:

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


You need -

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


or the regex way... just because it's there

if(obj.value.match(/[.].*[@]|[@].*[.]/))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜