473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 14015
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.