jQuery trim input val

Hello Experts,

Can someone help me understand why this if statement is not working?
Can i use the jQuery "trim" in this way on form input filed?

$(requiredInput).blur(function() {
  if (($.trim($(this).val() === "")) || ($.trim($(this).val().length <= 5))) {


Open in new window

Who is Participating?
haloexpertsexchangeConnect With a Mentor Commented:
trim requires a string not boolean values which is what you are giving it.

Maybe what you wanted was something more like this?
if (($.trim($(this).val() )=== "") || ($.trim($(this).val()).length <= 5)) {


Open in new window

RefaelAuthor Commented:
Perfect, Thank you haloexpertsexchange
Can you explain what this it mean a string here?  .val() )
.val() gets you the value from which ever element you are currently accessing with $(this).val();
This is not to be confused with the text of an element.
Inputs have both and do not necessarily have the same information in both or even have both filled in.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

RefaelAuthor Commented:
Hi haloexpertsexchange, thank you again.

So you basically it means that when i use "trim" i need to refer to the "text" (string) and not to the "value". by having this "val() )" . am i right?
RobOwner (Aidellio)Commented:
no - you need to use trim on a string.  you can also use it on val() / text() as they both return strings but you can't use it on a boolean such as true / false, which you were doing with your comparison:

...val() === "" <======== this returns a Boolean not a string
RefaelAuthor Commented:
Thank you tagit :-) now i understand.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.