3

What is wrong with this statement?

message = $("#contact-form").find(".long-text");

if(message.val().length < 15)
{
    message.css("border","2px solid red");
    alert("Your message must be more than 15 character long");
}

I get message.val is not a function error in Firebug? What could be the reason?

NEW

OK, now my form won't submit if I have more or less than 15 characters. Should I open a new question on this or should I continue on this one?

I have this inside:

$("#contact-form").submit(function(){
  message = $("#contact-form").find(".long-text");

    if(message.val().length < 15)
    {
        message.css("border","2px solid red");
        alert("Your message must be more than 15 character long");
        return false;
    }
    else {
        Post form with Ajax ..
    }
});
1
  • 1
    Each independent question is supposed to have its own thread. Commented Feb 20, 2010 at 23:00

4 Answers 4

4

As you can see in the syntax highlighter, the quotes went bogus :) There's a doublequote missing.

Besides, the val() only works on input elements (input, select, textarea and button). If it is indeed not an input element, then rather use text() instead. Further there's no such function as length(), you probably would like to use the length property.

I recommend to go get yourself through some basic jQuery tutorials and/or to go get a jQuery book.

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

Comments

0

Besides missing the end quote delimiter, here are some other notes

  • length is a property, not a method. It should be message.val().length
  • val() is also only useful for HTML input elements. You could try message.html().length or use text(), depending on your requirements

Comments

0

How about .length instead of length()

Comments

-1

You could try

message = $("#contact-form").find(".long-text").first();

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.