• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if( str.indexOf(" ") !== -1 )

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I needed some code to prevent someone from entering spaces into a web app I was building as a training exercise.

I came across this on Stackoverflow:

if( str.indexOf(" ") !== -1 ) {
and I added alert("Please enter a value.")
}

It works but I'm not sure what this line actually translates too.

Is it that all spaces have a value of -1? But then again, this line is asking for when it is Not -1 right?
Quoted spaces are strings correct? " " is a string.
What are we actually saying?

Thanks
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ken. Welcome to the Ranch!

Have you looked at the documentation for the indexOf method? For example, http://www.w3schools.com/jsref/jsref_indexof.asp.

You'll see it returns -1 if the search string is not found (otherwise it returns the position it was found at). So that condition is checking for whether str contains a space or not.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Let me introduce you to MDN (Mozilla Developer Network) and its documentation of String.

Look up the definition of the indexOf() method to find out what it does and why -1 is special.

[Edit: Doh! Beat by mere seconds to the draw!]
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(While w3schools has improved, I still prefer MDN as a resource).
 
Ken Guie
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Good tools!

I've used them before but I didn't think to look it up.

I just ran across this site and i thought i'd join and ask.

Thanks a lot !

 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to have you!

And yes, in JavaScript either double or single quotes delimit a string.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic