469,290 Members | 1,866 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

Validate text with space



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
7 13773
VK
if (myString.indexOf(' ') != -1) {
/* your code */
}
Jul 23 '05 #2
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


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
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
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
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
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.

Similar topics

7 posts views Thread by Alexandre | last post: by
reply views Thread by Ray Tayek | last post: by
13 posts views Thread by Eddie | last post: by
15 posts views Thread by simonmarkjones | last post: by
11 posts views Thread by jjbutera | last post: by
1 post views Thread by JoeZ | last post: by
6 posts views Thread by Tony Girgenti | last post: by
7 posts views Thread by Amit | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.