By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

Validate text with space

P: n/a


hi to all!
I just want to ask if how could i validate a text or string if it has a
space between characters?

Thanks in advance.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
VK
if (myString.indexOf(' ') != -1) {
/* your code */
}
Jul 23 '05 #2

P: n/a
On Sat, 18 Dec 2004 12:19:08 +0100, VK <sc**********@yahoo.com> wrote:

Please include quotes from the previous post.
if (myString.indexOf(' ') != -1) {
/* your code */
}


If the OP is trying to check if a space is between characters wouldn't the
index need to be one or greater?

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3

P: n/a


wow, thanks a lot!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4

P: n/a
Michael Winter wrote:
On Sat, 18 Dec 2004 12:19:08 +0100, VK <sc**********@yahoo.com> wrote:

Please include quotes from the previous post.
if (myString.indexOf(' ') != -1) {
/* your code */
}

If the OP is trying to check if a space is between characters wouldn't
the index need to be one or greater?


No. !1 = -1 means it was found in the string. But that doesn't satisfy
the OP's requirements:

' myStringWithOnlyALeadingSpace'

Passes that test but has no space "between" characters.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5

P: n/a
On Sat, 18 Dec 2004 14:40:24 -0500, Randy Webb <Hi************@aol.com>
wrote:

[snip]
!1 = -1 means it was found in the string.
Precisely. If the index was greater-than or equal to one, the space would
have to occur after the first character.

[snip]
Passes that test but has no space "between" characters.


The best solution would be a regular expression.

/^c+ c+$/ - Two compulsory words separated by a space.

/^c+( c+)?$/ - Two words separated by a space. Second word is
optional.

/^c+( c+)*$/ - Two or more words separated by a space. Only first
word is compulsory.

/^c+( c+)+$/ - Two or more words separated by a space. First two
words are compulsory.

In each case, c should be replaced by a character class or escape, such as
\w or [a-z]. You'd then use:

if(/^\w+( \w+)?$/.test(str)) {
/* String, str, passed */
}

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6

P: n/a
VK
Oh come on, guys...

The OP said (adjusting the grammar) : "how to find if a string has at least
one space between its characters"

if (myString.indexOf(' ') != -1) {
/* your code */
}
gives the right answer: yes, it does.

There are many other ways of course (escape, RegExp etc) but this one was
the most obvious.

Jul 23 '05 #7

P: n/a
VK wrote:
Oh come on, guys...

The OP said (adjusting the grammar) : "how to find if a string has at least
one space between its characters"

if (myString.indexOf(' ') != -1) {
/* your code */
}
gives the right answer: yes, it does.


No, it doesn't give the right answer. Test the following two strings:

myString = " String1"

myString = "String2 "

Both have spaces, both will pass the test, but neither has "at least one
space *between* characters".

A third, more obvious, example of failure is this:

myString = " "

What characters is that space between?

<--snip-->

And please, in the future, quote what you are replying to.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.