Solved

Spaces in Javascript

Posted on 2011-09-19
3
206 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
Comment Utility
0
 
LVL 82

Accepted Solution

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

Author Closing Comment

by:caf210
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now