Solved

Spaces in Javascript

Posted on 2011-09-19
3
208 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

821 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