Solved

Spaces in Javascript

Posted on 2011-09-19
3
209 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
3 Comments
 
LVL 11

Expert Comment

by:rajvja
ID: 36561186
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

856 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