[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Spaces in Javascript

Posted on 2011-09-19
3
Medium Priority
?
215 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 83

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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
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

873 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