473,396 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 14013
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alexandre | last post by:
Hi again, What's the best I can do to validate fields like date before send my datas. thx
0
by: Ray Tayek | last post by:
hi, fooling around with xmlspy (which seems pretty broken when *doing* xslt's). trying to validate in java using code from http://cermics.enpc.fr/doc/java/j2eetutorial-1.4/doc/JAXPSAX13.html...
13
by: Eddie | last post by:
I need to validate a text input field. I just want to say if user enters 93101 or 93102 or 93103 or 93105 or 93106 or 93107 or 93108 or 93109 or 93110 or 93111 or 93116 or 93117 or 93118 or...
15
by: simonmarkjones | last post by:
I want to validate my form using a BeforeUpdate event. However now that i call my code with a beforeupdate it wont let me go to next or previous records. What code should i put in o allow me...
11
by: jjbutera | last post by:
I know how to use the ErrorProvider in my winforms..or do I? I validate the values and set the ErrorProvider in the validating event. If not valid, I set e.Cancel = True. I clear the ErrorProvider...
1
by: JoeZ | last post by:
Hi all, I am using XMLValidatingReader to validate xml instance against schema. Now my question is: in the schema, it has the target name space. In the instance, it doesn't have the name...
6
by: Tony Girgenti | last post by:
Hello. Developing a VS2005, SP4, VB, .NET 2.0, ASP.NET 2.0, web site program using a calendar. I tried using the validators to see if i can validate dates using a calendar control. It does...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
7
by: Amit | last post by:
Dear Friends I need to write a Java Script for a string payment_code which comes populated from a text field , should contain only 0-9,A-Z,a-z,Space ' ',Hyphen '-',Full stop '.',Comma ',',Plus...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.