0

To check whether the email address is from gmail I tried the following jQuery code but I am getting Uncaught TypeError: Cannot call method 'indexOf' of undefined

CODE

$( document ).ready(function() {
  $("#sendit").click(
   function() {
      var str1 = $("#email").value;
      var str2 = "gmail.com";
      if(str1.indexOf(str2) != -1){
        alert("found");
      }
   });
});

Is there any error in my syntax?

1
  • If you had an error in your syntax, you would get a syntax error. You are trying to access a non-existing property of an object (.value), which will return undefined. That's a runtime error and a logic error in your code (but not a syntax error). Commented Jan 18, 2014 at 5:55

4 Answers 4

1

DEMO

Call the val() method.

var str1 = $("#email").val(); 

May be you are victim of jQuery Conflict. See if the code in this new fiddle works for you.

UPDATED FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

1

it should be

var str1 = $("#email").val();

$("#email") returns a jQuery wrapper object, it does not have the value property, it belongs to dom element. jQuery object has a method val() that returns the value.

4 Comments

@lock is there an element with id email
@lock can you share the html related to the element email
I just tried the this code $( document ).ready(function() { $("#userid").click( function() { var str1 = $("#email").val(); var str2 = "karthik"; if(str1.indexOf(str2) != -1){ alert("found"); } }); }); at jcubbooklinx.com/wp-login.php?action=login
there are other errors in the page... check your browser console
0

Should use val(), instead of value

var str1 = $("#email").val()

Comments

0

You should use val() instead of value

Change

var str1 = $("#email").value;

to

var str1 = $("#email").val();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.