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');
}
});
Any ideas?
:contains()
returns a jQuery object matching the criteria. It does not match value.Use
indexOf()
over.val()
,Also, in your
else{}
, use.val()
as text() does not set any value.But I'd suggest you use another element say
<span id="emailError">
Updated Fiddle
Try this: http://jsfiddle.net/xJtAV/71/. The string function
indexOf()
will search any string for whatever you'd like and return the index of the first character of the first occurence...String indexOf() docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
JS
HTML