Jquery check if input .val() contains certain characters
jquery
Solution
`indexOf` does not use a capital I at the start. Try this instead:
if (($("." + parentname.attr("class") + " #email").val().indexOf("@") != -1) && ($("." + parentname.attr("class") + " #email").val().indexOf(".") != -1))
{
email = 1;
}
Problem
I need to check if an input field on my page contains characters. This is for very basic email address validation so I only want to check if the text is not empty, and contains `@` and `.` characters. I've been trying this way: ``` if (($("." + parentname.attr("class") + " #email").val().contains("@")) && ($("." + parentname.attr("class") + " #email").val().contains("."))) { email = 1; } ``` Assuming a value of `me@this.com`, this code will throw the following error: Object me@this.com has no method 'contains' So I did some research and found that .contains is for DOM objects, not strings with a suggestion to try this: ``` if (($("." + parentname.attr("class") + " #email").val().IndexOf("@") != -1) && ($("." + parentname.attr("class") + " #email").val().IndexOf(".") != -1)) { email = 1; } ``` Which results in a similar error: Object me@this.com has no method 'IndexOf' I'm basically out of ideas here especially considering that the following code works as I want it to in another site I've made: ``` if ($("#contact-email").val().contains("yahoo.com")) { $(".errmsg").text("Domain yahoo.com has been banned due to excessive spam, please use another domain."); } ``` Can anyone suggest any other things I could try or, better yet, how to do this properly?