Avatar of Refael
RefaelFlag for United States of America asked on

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


Thanks!!!
jQueryJavaScript

Avatar of undefined
Last Comment
Refael

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
haloexpertsexchange

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Refael

Perfect, Thank you haloexpertsexchange
Can you explain what this it mean a string here?  .val() )
haloexpertsexchange

.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.
http://api.jquery.com/val/
http://api.jquery.com/text/
ASKER
Refael

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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Rob

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
ASKER
Refael

Thank you tagit :-) now i understand.