473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ 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 14026
VK
if (myString.index Of(' ') != -1) {
/* your code */
}
Jul 23 '05 #2
On Sat, 18 Dec 2004 12:19:08 +0100, VK <sc**********@y ahoo.com> wrote:

Please include quotes from the previous post.
if (myString.index Of(' ') != -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**********@y ahoo.com> wrote:

Please include quotes from the previous post.
if (myString.index Of(' ') != -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:

' myStringWithOnl yALeadingSpace'

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

--
Randy
comp.lang.javas cript 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.index Of(' ') != -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.index Of(' ') != -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.javas cript 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
2822
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
3157
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 (click on the Echo10.java link). i get an error saying that a doctype decl is required (see below). i get the same error whether or not i turn on name...
13
4768
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 93120 or 93121 or 93130 or 93140 or 93150 or 93160 or 93190 or 93199 or 93199 or 93401 or 93402 or 93403 or 93405 or 93406 or 93407 or 93408 or 93409...
15
4795
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 to do thi??
11
11747
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 in the validated event. Is there a way to know if all validated controls pass validation when the user clicks an OK button? In ASP.Net there's...
1
2247
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 space. I can not add the name space into the instance, if I could do that I won't have any problem. Currently I use XmlTextReader, XmlValidatingReader...
6
3368
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 not allow me to select the calendar control in the drop-down list for the ControlToValidate property. Is it possible to do client-side validation...
1
3980
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. Depends on the pull down list selection, I use script.aculo.us to validate the user input before submit and pass the necessary data, such as contact...
7
2229
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 '+' Characters other than these will not be allowed If a user enters characters other than the mentioned above , the alert message should be...
0
7701
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
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 we have to send another system
1
1223
muto222
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.