?
Solved

Spaces in Javascript

Posted on 2011-09-19
3
Medium Priority
?
212 Views
Last Modified: 2012-05-12
I'm working with a form where I am checking the values to ensure that the fields are not blank.

I can use a simple if statement, like this:

if(myValue == "")

Open in new window



But this doesn't account for multiple spaces. For example, if a user tabs in there and hits the space bar, it sees the values as " ", not as "".

So, right now, I have this:


if(myValue == "" || myValue == " " || myValue == "  " || myValue == "   " || myValue == "    ")

Open in new window



It works, but obviously, that's pretty "jury-rigged," don't you think?

And, of course, if they type more than four space, it won't work, because 5 spaces and up are not included in my IF statement.

Is there any means of checking that basically doesn't except "space" as a value?

(Of course, they could type in gibberish, or an X or something, so it's not like I'm expecting it to be fool proof. But it involves some scripting where the original description of what is intended in the field ("Your Phone Number," for example) is INSIDE as the initial value. So the application has some potential for them tabbing in and accidentally erasing the initial value, and I want a way to detect that it was "spaced over" so I can put it back in there.)

I'm also using jQuery on the page, so if there's a means of doing this in jQuery or straight javascript, either would be a viable solution for me.
0
Comment
Question by:caf210
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 1000 total points
ID: 36561286
You may use : if( myValue.replace(/\s/g,"").length == 0 )
0
 

Author Closing Comment

by:caf210
ID: 36561392
Yes, this is a great solution. Thank you!

rajvja, I see your suggestion is basically based on the same concept. I appreciate your input on this. Thank you for your help, too.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question