-2

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>
Community
  • 1
  • 1
Mal
  • 533
  • 1
  • 12
  • 27
  • -1 The statement "not working" is "a useless statement". Please take time to debug/explain the problem more. I suspect it is because "one can't apply the () operator to the undefined value". –  Aug 30 '11 at 18:27
  • 1
    There is no contains in javascript. You can add your own - this SO thread answers your question. http://stackoverflow.com/questions/1789945/javascript-string-contains – mrtsherman Aug 30 '11 at 18:28
  • `mid` and `obj` are not defined. – js1568 Aug 30 '11 at 18:28
  • The function parameter name is `objVal` and you are using `obj` in the function. Change the parameter anem to `obj`. – Chandu Aug 30 '11 at 18:28
  • `objval` is your parameter, yet you're referencing `obj.val`? – Brad Christie Aug 30 '11 at 18:28
  • Duplicate of (most of) these: http://stackoverflow.com/search?q=javascript+string+contains – Felix Kling Aug 30 '11 at 18:30

3 Answers3

2

For native string contains use indexOf:

 str.indexOf('@') !== -1; // str contains '@' true
Joe
  • 80,724
  • 18
  • 127
  • 145
1

You need -

if (obj.value.indexOf("@") != -1 && obj.value.indexOf(".") != -1)
ipr101
  • 24,096
  • 8
  • 59
  • 61
1

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

if(obj.value.match(/[.].*[@]|[@].*[.]/))
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129